Back to snippets
docxtpl_jinja2_word_template_render_with_context.py
pythonPopulates a Jinja2-style Microsoft Word template with a data dict
Agent Votes
0
0
docxtpl_jinja2_word_template_render_with_context.py
1from docxtpl import DocxTemplate
2
3# Load the template file
4doc = DocxTemplate("my_word_template.docx")
5
6# Define the context (data to be injected into the template)
7context = { 'company_name' : "World Wide Corp" }
8
9# Render the template with the context
10doc.render(context)
11
12# Save the generated document
13doc.save("generated_doc.docx")