Skip to main content

Performs a Mail Merge on docx (Microsoft Office Word) files

Project description

This project is currently unmaintained. Not recommended for production use.

docx Mail Merge

Build Status PyPI

Performs a Mail Merge on Office Open XML (docx) files. Can be used on any system without having to install Microsoft Office Word. Supports Python 2.7, 3.2 and up.

Installation

Installation with pip:

$ pip install docx-mailmerge

Usage

Open the file.

from mailmerge import MailMerge
document = MailMerge('input.docx')

List all merge fields.

print document.get_merge_fields()

Merge fields, supplied as kwargs.

document.merge(field1='docx Mail Merge',
               field2='Can be used for merging docx documents')

Merge table rows. In your template, add a MergeField to the row you would like to designate as template. Supply the name of this MergeField as anchor parameter. The second parameter contains the rows with key-value pairs for the MergeField replacements.

document.merge_rows('col1',
                    [{'col1': 'Row 1, Column 1', 'col2': 'Row 1 Column 1'},
                     {'col1': 'Row 2, Column 1', 'col2': 'Row 2 Column 1'},
                     {'col1': 'Row 3, Column 1', 'col2': 'Row 3 Column 1'}]

Starting in version 0.2.0 you can also combine these two separate calls into a single call to merge.

document.merge(field1='docx Mail Merge',
               col1=[
                   {'col1': 'A'},
                   {'col1': 'B'},
               ])

Starting in version 0.2.0 there’s also the feature for template mergingg. This creates a copy of the template for each item in the list, does a merge, and separates the them by page breaks.

document.merge_pages([
    {'field1': "Foo", 'field2: "Copy #1"},
    {'field1': "Bar", 'field2: "Copy #2"},
])

Write document to file. This should be a new file, as ZipFile cannot modify existing zip files.

document.write('output.docx')

Todo / Wish List

  • Preserve formatting of the merge field, currently it defaults to the formatting of the containing text.

  • Image merging.

Contributing

  • Fork the repository on GitHub and start hacking

  • Create / fix the unit tests

  • Send a pull request with your changes

Unit tests

In order to make sure that the library performs the way it was designed, unit tests are used. When providing new features, or fixing bugs, there should be a unit test that demonstrates it. Run the test suite:

python -m unittest discover

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docx-mailmerge-0.2.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

docx_mailmerge-0.2.0-py2.py3-none-any.whl (6.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file docx-mailmerge-0.2.0.tar.gz.

File metadata

File hashes

Hashes for docx-mailmerge-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9170616355820db9885340633674b23f2b9e352425f8331885461f073552eb03
MD5 f995c0b3b5194b20b1ea7a8f0970c7b0
BLAKE2b-256 6e1bbd6fee14ba8d2beb8db666ab4dd9d72b58dd97b4ce10d8fad70fe7a0f58e

See more details on using hashes here.

File details

Details for the file docx_mailmerge-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for docx_mailmerge-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bcd8ffec04a28e8c35020ff3de2f31dd59c38537a17f0bc4e628120cad1fa210
MD5 f9597185d5a7e08391829b2203527e1a
BLAKE2b-256 5d38665e7ef0b258e918a5952ba1cdc26ded029506e85a26bde0f9286fda71a1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page