docxtpl-ng¶
Use a .docx file as a Jinja2 template.
Design the document in Word — styles, tables, headers, page layout, the lot —
write {{ customer.name }} where the data goes, and render it from Python.
from docxtpl import Template
tpl = Template("invoice.docx")
tpl.render({"customer": customer, "lines": lines, "total": total})
tpl.save("invoice-2026-0042.docx")
Alpha
The whole feature set is implemented and tested — rendered documents are
validated against the ISO/IEC 29500 schemas, and compared against what
docxtpl itself produces — but it has not been run against a large corpus
of real templates or used in anger. The 0. major says the same: expect to
find things, and expect the odd rough edge to be filed off in a way that
changes behaviour.
Where to start¶
- Installing
- Quickstart — a template and a render, end to end
- Template syntax — the tags you write in the document
- Migrating from docxtpl — swap the dependency, change no code
By feature¶
- Rich text and hyperlinks — formatting that comes from the data
- Images — inline pictures, and replacing ones the template has
- Fragments and assembly — content whose structure varies
- Tables — repeating rows, merges and shading
- Headers, footers and properties — everything that is not the body
- Preformatted text — multi-line text with its indentation
- Inspecting a template — what does this template need?
- Security — a .docx template is executable input; read this before rendering one you did not write
Part of the -ng suite¶
docxtpl-ng builds on python-docx-ng,
which provides the document model. Anything the template cannot express, you can
reach for directly: Template.document is an ordinary python-docx-ng
Document, and a Fragment lets you build content
with that API and drop it into the template at a tag.