docx-mailmerge2
Performs a Mail Merge on docx (Microsoft Office Word) files
What it is and what it does
docx-mailmerge2 is a Python library that automates the population of Microsoft Word templates by replacing merge fields with data. It reads docx files, identifies MERGEFIELD placeholders, substitutes them with values you provide, and writes the result to a new docx file—all without needing Microsoft Office installed. The library handles simple field replacement, table row templating (expanding or replacing rows based on data lists), and template duplication with page or section breaks between copies.
The package supports field formatting for numeric, text, conditional, and date values, and can work with nested fields like INCLUDEPICTURE and IF statements (though these may require manual field updates in Word afterward). It offers configuration options to control empty table removal, field preservation, automatic field updates on document open, and conditional field evaluation. The single runtime dependency is lxml, making it lightweight and portable across systems.
Use it for:
- Generate personalized letters or invoices by merging customer data into Word templates without manual editing.
- Create bulk reports by duplicating a template for each data record and separating them with page breaks.
- Populate dynamic tables in docx files by expanding template rows with lists of records.
- Automate document generation in server-side applications where Microsoft Office is unavailable.
- Preserve merge fields in output documents for later re-merging or manual updates in Word.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Performs mail merge operations on docx files by replacing merge fields with data, without requiring Microsoft Office to be installed.
Yes. The package is actively maintained, has no known vulnerabilities, carries a permissive MIT license, and solves a real problem (docx templating without Office) with low install friction. It is suitable for production use in document automation workflows, particularly where server-side generation or batch processing is needed.
Install
docx-mailmerge2 on PyPI
pip
pip install docx-mailmerge2uv
uv add docx-mailmerge2poetry
poetry add docx-mailmerge2Installing docx-mailmerge2
Before you install
Low friction install with a single lightweight dependency (lxml). Actively maintained with recent releases; last commit 2026-06-19 and latest release 2025-12-28. Supports Python 3.7 through 3.13.
License in practice
MIT license (permissive) allows free use, modification, and distribution with minimal restrictions—suitable for both open-source and commercial projects.
Quickstart
pip install docx-mailmerge2
from docx_mailmerge2 import MailMerge
with MailMerge('input.docx') as document:
document.merge(field1='value1', field2='value2')
document.write('output.docx')
Merge fields in the docx template must be created using CTRL-F9 (or CMD-F9 on macOS) in Microsoft Word, not as ordinary text.
Verify before relying
- Whether nested field updates (e.g., INCLUDEPICTURE, IF fields) work without opening the output in Microsoft Word to trigger F9 refresh.
- Performance characteristics when merging large numbers of rows or templates in a single operation.
- Compatibility with docx files created by non-Microsoft tools (LibreOffice, Google Docs).
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.7) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — lxml |
| Maintenance | actively maintained — 229 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 133,147/month — #11,525 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: docx_mailmerge2-1.0.1-py3-none-any.whl
Keywords: docx, office, openxml, word
Tags
More Text Processing packages
A drop-in replacement for Python's standard…
permissive · top 100 on PyPI
pyparsingpyparsing provides a library for building text…
permissive · top 1,000 on PyPI
docutilsDocutils converts plaintext documentation in…
unclear · top 1,000 on PyPI
RapidFuzzRapidFuzz provides fast fuzzy string matching…
permissive · top 1,000 on PyPI
tinycss2tinycss2 parses CSS strings into token and…
permissive · top 1,000 on PyPI
llama-parseLlamaParse parses complex documents (PDFs,…
permissive · top 1,000 on PyPI
docx-mailmergePerforms mail merge operations on docx files by…
permissive · top 15,000 on PyPI
docxtplGenerates Word documents (.docx) from templates…
copyleft · top 5,000 on PyPI
docxcomposeConcatenates and merges multiple Microsoft Word…
permissive · top 5,000 on PyPI
docxCreates, reads, and writes Microsoft Office…
unclear · top 15,000 on PyPI
doc2docxConverts legacy .doc files to modern .docx…
permissive · top 15,000 on PyPI
python-docx-ml6Reads, creates, and updates Microsoft Word…
permissive · top 15,000 on PyPI
python-docxpython-docx reads, creates, and modifies…
permissive · top 1,000 on PyPI
html-for-docxConverts HTML content to Word documents…
permissive · top 15,000 on PyPI
aspose-wordsAspose.Words for Python is a document…
unclear · top 15,000 on PyPI
docx2pdfConverts DOCX files to PDF on Windows or macOS…
permissive · top 5,000 on PyPI