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")
- Documentation: https://toxicphreak.github.io/docxtpl-ng/
- Repo: https://github.com/toxicphreAK/docxtpl-ng
- Releases: https://github.com/toxicphreAK/docxtpl-ng/releases
- PyPI: https://pypi.org/project/docxtpl-ng/
Status
Alpha. The whole feature set is implemented and tested — 269 tests, with
rendered documents validated against the ISO/IEC 29500 schemas and compared
against what docxtpl itself produces — but it has not been run over a large
corpus of real templates or used in anger. The 0. major says the same thing:
expect to find things, and expect the odd rough edge to be filed off in a way
that changes behaviour. Follow the
milestones for
progress.
Installing
pip install docxtpl-ng
or, with uv:
uv add docxtpl-ng
Note: the importable package is
docxtpl, notdocxtpl_ng— useimport docxtpl.docxtpl-nganddocxtpltherefore cannot be installed side by side.
Python 3.9 through 3.14. The only runtime dependencies are python-docx-ng for the document model and Jinja2 for the templating.
Two APIs
docxtpl-ng exports two public surfaces over one engine.
The modern API — Template, Text, Fragment, Image, Preformatted —
is typed throughout and keyword-only where an argument is easy to misplace. Use
it for new code.
The compatibility API — DocxTemplate, RichText, R, Subdoc,
InlineImage, Listing — matches the names and signatures published by the
docxtpl distribution. The package this
distribution installs is called docxtpl as well, so an existing project
migrates by swapping the dependency and changing nothing else:
-docxtpl>=0.16
+docxtpl-ng>=0.1.0
from docxtpl import DocxTemplate, RichText, InlineImage # unchanged
The two cannot be installed side by side — they claim the same import name — so
remove docxtpl first.
Your .docx templates need no changes at all: the tag dialect — {{ var }},
{%p %}, {%tr %}, {%tc %}, {%r %}, {% colspan %}, {% cellbg %},
{% hm %}, {% vm %} — is reproduced exactly. The compatibility surface is
supported indefinitely and is not deprecated.
Relationship to docxtpl
docxtpl (repository python-docx-template) is the project that established
this idea, and it deserves the credit for it. docxtpl-ng is an independent
implementation, not a fork: it shares no code with it, and it is MIT licensed
rather than LGPL-2.1.
It exists because docxtpl depends on the python-docx distribution, which
cannot be installed alongside python-docx-ng, and because the LGPL made
absorbing it into the -ng suite impossible. Rewriting was the way to get a
templating layer that is part of the suite, permissively licensed, and built on
the typed element tree rather than on regular expressions over serialised XML.
How it works
Four phases, of which only one sees text:
| Phase | What it does |
|---|---|
| 1. Coalesce | Merge adjacent runs Word split mid-tag, so {{ cust+omer }} is one tag again |
| 2. Compile | Hoist {%tr %}/{%p %} control tags onto the element they name; replace table directives with sentinels; serialise once |
| 3. Render | Jinja2, with XML autoescaping |
| 4. Rebuild | Re-parse into typed elements, expand \n/\t/\f into w:br/w:tab, resolve merges and shading against the real table |
Structural decisions are made where the schema is known, so a rendered document is well-formed by construction. See docs/dev/architecture.md.
Contributing
uv sync
uv run pytest -q
uv run ruff check .
Requires uv.
Licence
MIT. See LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file docxtpl_ng-0.2.0.tar.gz.
File metadata
- Download URL: docxtpl_ng-0.2.0.tar.gz
- Upload date:
- Size: 121.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fb338ea2ceb7e2cd8e33dd64fd9cb50a1cd31b64538837a22de26839e1bb306
|
|
| MD5 |
2b58b60afa0912e08f4d422ea9b9132f
|
|
| BLAKE2b-256 |
ac2e880ec2e7128b87ab1f2b20369879d0a72ff4c96a4d4cf032435703c90708
|
Provenance
The following attestation bundles were made for docxtpl_ng-0.2.0.tar.gz:
Publisher:
python-publish.yml on toxicphreAK/docxtpl-ng
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
docxtpl_ng-0.2.0.tar.gz -
Subject digest:
8fb338ea2ceb7e2cd8e33dd64fd9cb50a1cd31b64538837a22de26839e1bb306 - Sigstore transparency entry: 2367207308
- Sigstore integration time:
-
Permalink:
toxicphreAK/docxtpl-ng@b867d0c1ce207dff66dfd93191f7ef163dd2893a -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/toxicphreAK
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b867d0c1ce207dff66dfd93191f7ef163dd2893a -
Trigger Event:
push
-
Statement type:
File details
Details for the file docxtpl_ng-0.2.0-py3-none-any.whl.
File metadata
- Download URL: docxtpl_ng-0.2.0-py3-none-any.whl
- Upload date:
- Size: 82.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee7da6466c8b4d068449f344faee6761811fde2ad48153c7d5f7b1cd8ebc8422
|
|
| MD5 |
2116bfccf523fb79106fc7df1b3cd8e3
|
|
| BLAKE2b-256 |
4fa79ce2f6a9d0aeef5ed10fdb2fe1b8b73708482e8a0f12ea57fe69b5843e56
|
Provenance
The following attestation bundles were made for docxtpl_ng-0.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on toxicphreAK/docxtpl-ng
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
docxtpl_ng-0.2.0-py3-none-any.whl -
Subject digest:
ee7da6466c8b4d068449f344faee6761811fde2ad48153c7d5f7b1cd8ebc8422 - Sigstore transparency entry: 2367207718
- Sigstore integration time:
-
Permalink:
toxicphreAK/docxtpl-ng@b867d0c1ce207dff66dfd93191f7ef163dd2893a -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/toxicphreAK
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b867d0c1ce207dff66dfd93191f7ef163dd2893a -
Trigger Event:
push
-
Statement type: