Docform
Fill document templates with data.
Install · Quickstart · Example · Docs · Roadmap · Contributing
Design the document in Word with whatever layout, styles, headers and images you need, and put Jinja2 tags where the data goes. Docform renders it into a finished .docx, so you can generate contracts, invoices, reports, certificates and letters in bulk from Python.
Install
pip install docform
# with subdocument support
pip install "docform[subdoc]"
Quickstart
In Word, write a template like this and save it as invoice_template.docx:
Invoice for {{ customer }}
{%tr for item in items %}
| {{ item.name }} | {{ item.qty }} |
{%tr endfor %}
Total: {{ total }}
Then render it:
from docform import DocxTemplate
doc = DocxTemplate("invoice_template.docx")
doc.render({
"customer": "Acme",
"items": [{"name": "Widget", "qty": 3}, {"name": "Gadget", "qty": 1}],
"total": "$120.00",
})
doc.save("invoice_acme.docx")
Everything you set up in Word, like fonts, logos and page layout, stays intact. Only the tags are replaced.
How it works
flowchart LR
T["Word template<br/>(.docx with Jinja2 tags)"] --> R{{"Docform<br/>render()"}}
D["Your data<br/>(dict, JSON, database rows)"] --> R
R --> O["Finished document<br/>(.docx)"]
Example
This invoice comes from examples/invoice. The template on the left is a normal Word file. Running python render.py turns it into the document on the right.
Template (invoice_template.docx) |
Rendered (invoice_acme.docx) |
|---|---|
A few things to notice in the template:
{%tr for item in items %}and{%tr endfor %}sit in their own table rows. Docform repeats the row between them once per item and removes the tag rows.{%p if paid %}works the same way for paragraphs, so the payment note switches between "PAID" and a reminder.- Formatting on a tag, like the bold blue
{{ total }}, carries over to the value.
Features
- Full Jinja2 syntax: variables, conditions, loops, filters and custom filters
- Paragraph, table row, table cell and run level tags (
{%p %},{%tr %},{%tc %},{%r %}) that remove the surrounding Word element so loops don't leave empty rows behind - Rich text with colors, fonts, bold, italic, links and right-to-left text
- Inline images, plus replacing pictures and embedded objects already in the template
- Subdocuments: build parts with python-docx or merge in existing
.docxfiles - Headers, footers, footnotes and document properties
- Cell background colors and horizontal and vertical cell merges
- Listing undeclared template variables before rendering
- A command line interface for JSON-driven rendering
Command line
python -m docform template.docx data.json output.docx
Use -o to overwrite an existing output file and -q for quiet mode.
Sandboxed rendering
Templates run Jinja2 code, so by default Docform renders them in Jinja2's SandboxedEnvironment. Normal template features work, but a template can't reach Python internals like __globals__ or __class__, and trying raises jinja2.exceptions.SecurityError.
If you pass your own jinja_env, Docform uses it as is. Use a SandboxedEnvironment there too unless every template is trusted:
from jinja2.sandbox import SandboxedEnvironment
env = SandboxedEnvironment(autoescape=True)
env.filters["shout"] = str.upper
doc.render(context, jinja_env=env)
To turn the sandbox off for templates you fully trust, pass jinja_env=jinja2.Environment(), or --unsafe on the command line.
Formats
Word (.docx) is the only format today. Each format lives in its own module, so new ones can be added without breaking existing code:
from docform.docx import Template
Documentation
The full template syntax and API reference are in docs/. examples/ has complete, runnable examples, and tests/ has a small script for every feature.
Roadmap
Next up are the stuck fixes from python-docx-template, type hints and safer defaults for untrusted templates, and later Excel and PowerPoint support. See ROADMAP.md.
Contributing
Bug reports, fixes and features are welcome. CONTRIBUTING.md covers setup, tests and how to send a pull request, and everyone taking part follows the code of conduct. Please report security problems privately as described in SECURITY.md.
Adopters
Using Docform in production or in an open source project? Add yourself to ADOPTERS.md.
Citing
If you use Docform in research, please cite it. GitHub's "Cite this repository" button reads CITATION.cff.
Maintainer
Docform is maintained by Srinivas Chippagiri.
Credits
Docform started as a fork of python-docx-template by Eric Lapouyade and builds on python-docx and Jinja2.
License
Metadata
Release files for docform 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| docform-1.0.2.tar.gz | 2.1 MB | With provenance | Details for docform-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| docform-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for docform-1.0.2-py3-none-any.whl |
Total release size: 2.1 MB
Release files / docform-1.0.2.tar.gz
| Download URL | docform-1.0.2.tar.gz |
|---|---|
| Size | 2.1 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e22194247d7daf38073614fa91e96e4ef986012f64d789d5905f79860804bc3a
|
|
BLAKE2b-256 checksum How to use checksums |
7e61ae933a8d0c5f257d6db316612cc716e6c4836bbdf56e376264f1db34f437
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.
Transparency logRelease files / docform-1.0.2-py3-none-any.whl
| Download URL | docform-1.0.2-py3-none-any.whl |
|---|---|
| Size | 36.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d589ff86b5d6e0917fb93e97e879ee6e29a3ce253f848d496b1fcc497c59ee62
|
|
BLAKE2b-256 checksum How to use checksums |
681debccf3d74ee7e12013d1bdaf31a5eccbbdcec6a2531b2a92a8c4bdb8014a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.
Transparency logSecurity for docform 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for docform-1.0.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.