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.2.0.tar.gz (121.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.2.0-py3-none-any.whl (82.1 kB view details)

Uploaded Python 3

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

Hashes for docxtpl_ng-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8fb338ea2ceb7e2cd8e33dd64fd9cb50a1cd31b64538837a22de26839e1bb306
MD5 2b58b60afa0912e08f4d422ea9b9132f
BLAKE2b-256 ac2e880ec2e7128b87ab1f2b20369879d0a72ff4c96a4d4cf032435703c90708

See more details on using hashes here.

Provenance

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

Hashes for docxtpl_ng-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee7da6466c8b4d068449f344faee6761811fde2ad48153c7d5f7b1cd8ebc8422
MD5 2116bfccf523fb79106fc7df1b3cd8e3
BLAKE2b-256 4fa79ce2f6a9d0aeef5ed10fdb2fe1b8b73708482e8a0f12ea57fe69b5843e56

See more details on using hashes here.

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

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

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.1

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