Skip to main content

docxtpl-ng

PyPI Downloads

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")

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, not docxtpl_ng — use import docxtpl. docxtpl-ng and docxtpl therefore 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 APITemplate, Text, Fragment, Image, Preformatted — is typed throughout and keyword-only where an argument is easy to misplace. Use it for new code.

The compatibility APIDocxTemplate, 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

docxtpl_ng-0.1.1.tar.gz (81.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docxtpl_ng-0.1.1-py3-none-any.whl (63.7 kB view details)

Uploaded Python 3

File details

Details for the file docxtpl_ng-0.1.1.tar.gz.

File metadata

  • Download URL: docxtpl_ng-0.1.1.tar.gz
  • Upload date:
  • Size: 81.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for docxtpl_ng-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b066749b0354abbe99e35e3b592617c18396f0a292d3b47bc6b35d155cfc5db9
MD5 f226cf354d179ed2d2b9e2e0206db663
BLAKE2b-256 a44963920399f74fc40a371ed12b309a6ad2f1ef4c3593ac1c30092b0a18aa55

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxtpl_ng-0.1.1.tar.gz:

Publisher: python-publish.yml on toxicphreAK/docxtpl-ng

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file docxtpl_ng-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: docxtpl_ng-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 63.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for docxtpl_ng-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9aee585d5d0bf15b6f762b80e4a72fe0414e576980b577fed2e5fa239f44a9a5
MD5 2a87f246c77a45b72fe753a8fd452e12
BLAKE2b-256 1a411c73d0964078260a9ea24a88cab385d3e4ba4af5b764aef4d1be083f0582

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxtpl_ng-0.1.1-py3-none-any.whl:

Publisher: python-publish.yml on toxicphreAK/docxtpl-ng

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.2.0

2 files

This release

0.1.1 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page