skillfed

docxtpl

Python docx template engine

docxtpl v0.20.2 3.4M downloads/30d#2,639 on PyPI2,690
Copyleft license LGPL-2.1-only Active released

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 docxtpl

uv

uv add docxtpl

poetry

poetry add docxtpl

Installing 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

Development Status :: 4 - BetaIntended Audience :: DevelopersProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9

Tags

docx template engineword document generationjinja2 docx templatingdynamic word documentsdocx mail mergepython word automationgenerate docx from template
document-generationtemplate-engineoffice-automation

More Text Processing packages