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.0.tar.gz (76.6 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.0-py3-none-any.whl (61.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: docxtpl_ng-0.1.0.tar.gz
  • Upload date:
  • Size: 76.6 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.0.tar.gz
Algorithm Hash digest
SHA256 281ed8dc69d9429dbb1fca74afa540d5bf9fd7cc19b3c7f5e7c8f5a1cbadf15b
MD5 c4216577bbfc6c1fecbd512c97045f9e
BLAKE2b-256 2c5397c686797c7fcf1ccc5dc1f447e6829accd5945d166b1cd4ce375e0a86f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxtpl_ng-0.1.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: docxtpl_ng-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 61.4 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b8b2df8293a558b1eacfbb96543b7a8fbe7f34d724a16d48eb241e04d6f5347
MD5 debfe6501f99b89e549d3de63273b93b
BLAKE2b-256 a6f31124d011adaa778f701890e805d0d56b85af27ebc0fe4304b39655710552

See more details on using hashes here.

Provenance

The following attestation bundles were made for docxtpl_ng-0.1.0-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

0.1.1

2 files

This release

0.1.0 This release

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