Top Related Projects
Create and modify Word documents with Python
Quick Overview
Python-docx-template is a Python library that allows users to generate Microsoft Word documents (.docx) from templates. It extends the python-docx library by adding templating features, enabling users to create dynamic Word documents with placeholders for data, loops, and conditional statements.
Pros
- Easy integration with existing Word templates
- Supports complex document structures, including tables, lists, and images
- Allows for dynamic content generation using Jinja2-like syntax
- Compatible with Python 2.7 and 3.x
Cons
- Limited formatting options compared to direct manipulation with python-docx
- May have performance issues with very large documents or complex templates
- Requires basic knowledge of Jinja2 templating syntax
- Documentation could be more comprehensive for advanced use cases
Code Examples
- Basic usage:
from docxtpl import DocxTemplate
doc = DocxTemplate("my_template.docx")
context = {'company_name': 'Acme Corp', 'amount': 123.45}
doc.render(context)
doc.save("generated_doc.docx")
- Using loops in templates:
from docxtpl import DocxTemplate
doc = DocxTemplate("template_with_table.docx")
context = {
'items': [
{'name': 'Apple', 'price': 1.0},
{'name': 'Banana', 'price': 0.5},
{'name': 'Orange', 'price': 0.75}
]
}
doc.render(context)
doc.save("generated_table.docx")
- Conditional statements in templates:
from docxtpl import DocxTemplate
doc = DocxTemplate("template_with_conditions.docx")
context = {
'is_member': True,
'name': 'John Doe',
'discount': 15
}
doc.render(context)
doc.save("generated_conditional.docx")
Getting Started
To get started with python-docx-template:
-
Install the library:
pip install docxtpl
-
Create a Word document template with placeholders using
{{ }}
syntax. -
Use the following basic code structure:
from docxtpl import DocxTemplate doc = DocxTemplate("path/to/your/template.docx") context = {'variable1': 'value1', 'variable2': 'value2'} doc.render(context) doc.save("output_document.docx")
-
Run your Python script to generate the document.
Competitor Comparisons
Create and modify Word documents with Python
Pros of python-docx
- More comprehensive API for creating and manipulating Word documents from scratch
- Better documentation and wider community support
- Actively maintained with regular updates and bug fixes
Cons of python-docx
- Steeper learning curve for complex document manipulation
- Less intuitive for template-based document generation
- Requires more code to achieve template-like functionality
Code Comparison
python-docx:
from docx import Document
document = Document()
document.add_heading('Document Title', 0)
document.add_paragraph('A plain paragraph having some bold and some italic.')
document.save('document.docx')
python-docx-template:
from docxtpl import DocxTemplate
doc = DocxTemplate("my_template.docx")
context = { 'title': 'Document Title', 'content': 'A plain paragraph having some bold and some italic.' }
doc.render(context)
doc.save("generated_doc.docx")
The python-docx-template code is more concise for template-based document generation, while python-docx offers more granular control over document creation and manipulation.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
==================== python-docx-template
Use a docx as a jinja2 template
Introduction
This package uses 2 major packages :
- python-docx for reading, writing and creating sub documents
- jinja2 for managing tags inserted into the template docx
python-docx-template has been created because python-docx is powerful for creating documents but not for modifying them.
The idea is to begin to create an example of the document you want to generate with microsoft word, it can be as complex as you want : pictures, index tables, footer, header, variables, anything you can do with word. Then, as you are still editing the document with microsoft word, you insert jinja2-like tags directly in the document. You save the document as a .docx file (xml format) : it will be your .docx template file.
Now you can use python-docx-template to generate as many word documents you want from this .docx template and context variables you will associate.
Documentation
Please, read the doc <http://docxtpl.readthedocs.org>
_
Other projects
If you like python-docx-template, please have a look at some of my other projects :
django-listing <https://github.com/elapouya/django-listing>
_ : A listing/table library on steroid for Djanopython-textops3 <https://github.com/elapouya/python-textops3>
_ : Chainable text operationsdjango-robohash-svg <https://github.com/elapouya/django-robohash-svg>
_ : Create svg robots avatars
Top Related Projects
Create and modify Word documents with Python
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot