docx-mailmerge

raw JSON →
0.5.0 verified Mon Apr 27 auth: no python maintenance

Performs a Mail Merge on docx (Microsoft Office Word) files using Python. Current version: 0.5.0. Low release cadence (last release in 2018).

pip install docx-mailmerge
error ImportError: No module named 'mailmerge'
cause Incorrect package name used in import; package is 'mailmerge' not 'docx_mailmerge'.
fix
Run: from mailmerge import MailMerge
error KeyError: 'field_name'
cause The merge field name provided does not exist in the template or is not extracted correctly.
fix
List fields with: doc.get_merge_fields() and use exact names.
breaking Python 2 support was dropped in 0.4.0. Version 0.5.0 requires Python 3.5+.
fix Use Python 3.5 or higher.
gotcha Merge field names must match exactly (case-sensitive). Common mistake: using spaces instead of underscores.
fix Verify field names in the template by inspecting XML or using MailMerge.get_merge_fields().
gotcha The merge() method overwrites the internal document; subsequent merge calls will lose previous merges unless you re-open the template.
fix Use a context manager or re-initialize MailMerge for each merge iteration.

Open a DOCX template, merge fields, and save output.

from mailmerge import MailMerge

doc = MailMerge('input.docx')
doc.merge(field1='value1', field2='value2')
doc.write('output.docx')