docxtpl
Python docx template engine
What it is and what it does
docxtpl bridges the gap between python-docx (which excels at creating documents from scratch) and the need to modify or populate existing Word documents. You design your document layout in Microsoft Word—complete with formatting, images, headers, footers, and tables—then insert Jinja2 template tags directly into the document text. Save it as a .docx file, and docxtpl reads that template, processes the Jinja2 tags with your context variables, and generates a new Word document with the populated content.
The package is built on three core dependencies: python-docx handles the underlying .docx file manipulation, jinja2 provides the templating engine for tag processing, and lxml handles the XML parsing that Word documents rely on. This makes it well-suited for mail-merge workflows, report generation, and document automation where you want fine-grained control over the final document's appearance without writing complex layout code.
Use it for:
- Generate personalized letters or contracts by inserting recipient data into a pre-formatted Word template.
- Create dynamic reports with tables, charts, and formatted sections populated from database queries or API responses.
- Automate invoice or receipt generation with company branding and variable line items.
- Produce batch documents (certificates, labels, forms) with consistent styling and variable content.
- Build document workflows where non-technical users design templates in Word and developers populate them programmatically.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Generates Word documents (.docx) from templates by embedding Jinja2 tags into a Word document and populating them with context variables at runtime.
Yes. The package is actively maintained, has low install friction, supports modern Python versions, and solves a real gap in Word document automation. The LGPL-2.1-only license is permissive for most use cases but requires attention if you plan to distribute modified versions. No known vulnerabilities. Install it if you need to generate Word documents from templates with dynamic content.
Install
docxtpl on PyPI
pip
pip install docxtpluv
uv add docxtplpoetry
poetry add docxtplInstalling docxtpl
Before you install
Low install friction with three stable runtime dependencies (python-docx, jinja2, lxml). Active maintenance with a recent release on 2025-11-13 and last commit on 2026-07-07; supports Python 3.7 through 3.13.
License in practice
Licensed under LGPL-2.1-only (copyleft). Any derivative work or modification must be distributed under the same license; proprietary use requires careful review of your distribution model.
Quickstart
pip install docxtpl
from docxtpl import DocxTemplate
doc = DocxTemplate('template.docx')
context = {'name': 'John'}
doc.render(context)
doc.save('output.docx')
Requires a .docx template file with embedded Jinja2 tags; the template must be created in Microsoft Word or compatible editor before use.
Verify before relying
- Whether complex nested Jinja2 constructs (loops, conditionals) are fully supported within Word document structure constraints.
- Performance characteristics when rendering large documents or templates with many variables.
- Compatibility with Word features like tracked changes, comments, or form fields in templates.
Package facts
| License | LGPL-2.1-only (copyleft) |
| Python support | supports the current Python release (>=3.7) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 3 — python-docx, jinja2, lxml |
| Maintenance | actively maintained — 274 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 3,387,086/month — #2,639 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: docxtpl-0.20.2-py3-none-any.whl
Keywords: jinja2
Tags
More Text Processing packages
A drop-in replacement for Python's standard…
permissive · top 100 on PyPI
pyparsingpyparsing provides a library for building text…
permissive · top 1,000 on PyPI
docutilsDocutils converts plaintext documentation in…
unclear · top 1,000 on PyPI
RapidFuzzRapidFuzz provides fast fuzzy string matching…
permissive · top 1,000 on PyPI
tinycss2tinycss2 parses CSS strings into token and…
permissive · top 1,000 on PyPI
llama-parseLlamaParse parses complex documents (PDFs,…
permissive · top 1,000 on PyPI
docxCreates, reads, and writes Microsoft Office…
unclear · top 15,000 on PyPI
htmldocxConverts HTML content into Microsoft Word…
permissive · top 5,000 on PyPI
html-for-docxConverts HTML content to Word documents…
permissive · top 15,000 on PyPI
python-docx-ml6Reads, creates, and updates Microsoft Word…
permissive · top 15,000 on PyPI
docx-mailmergePerforms mail merge operations on docx files by…
permissive · top 15,000 on PyPI
docx-mailmerge2Performs mail merge operations on docx files by…
permissive · top 15,000 on PyPI
docx2txtExtracts text, headers, footers, hyperlinks,…
unclear · top 5,000 on PyPI
python-docxpython-docx reads, creates, and modifies…
permissive · top 1,000 on PyPI
jinja2-simple-tagsProvides base classes to create custom Jinja2…
permissive · top 5,000 on PyPI
mammothConverts Microsoft Word .docx documents to…
permissive · top 5,000 on PyPI