Performs a Mail Merge on docx (Microsoft Office Word) files
Project description
基于Bouke的 Docx-mailmerge: 1、merge与merge_rows可以将所有符合的全部替换 2、基于TNGPS提供的方式,实现图片的替换(title为field名称,descr如果为deletable,没有匹配数据则删除) Base on Bouke’s Docx-mailmerge(3q): 1. enable merge_rows all required table, not only the first. 2. fixed the nested table rows merge bug.
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.3 and up.
Installation
Installation with pip:
$ pip install docx-mailmerge-hxd
Usage
Open the file.
from mailmerge import MailMerge with MailMerge('input.docx') as document: ...
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 merging. This creates a copy of the template for each item in the list, does a merge, and separates them by page or section breaks (see function documentation).
When using this feature, make sure you don’t use comments, footnotes, bookmarks, etc. This is because these elements have an id attribute, which must be unique. This library does not handle this, resulting in invalid documents.
document.merge_templates([ {'field1': "Foo", 'field2: "Copy #1"}, {'field1': "Bar", 'field2: "Copy #2"}, ], separator='page_break')
Write document to file. This should be a new file, as ZipFile cannot modify existing zip files.
document.write('output.docx')
See also the unit tests and this nice write-up Populating MS Word Templates with Python on Practical Business Python for more information and examples.
Todo / Wish List
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
Credits
This library was written by Bouke Haarsma and contributors.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file docx-mailmerge-hxd-0.6.tar.gz
.
File metadata
- Download URL: docx-mailmerge-hxd-0.6.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c3f130b6cc82da1baeb64fd0489c7ee099f193c6e1690ebca1e8686f0510769 |
|
MD5 | 600cd333792eec7977ee59f1903458fc |
|
BLAKE2b-256 | 547c3b142b13dfb6ecf386e56c39a8fddc9c5e4e800c986e7f896f0aee1b54f5 |
File details
Details for the file docx_mailmerge_hxd-0.6-py2.py3-none-any.whl
.
File metadata
- Download URL: docx_mailmerge_hxd-0.6-py2.py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ff65038e750ccf22c505af6c9ac9a96a4d1e75c4be8075a5f52772c11d5c131 |
|
MD5 | fc470d1084aca380f22de7342fdd5d53 |
|
BLAKE2b-256 | 970c57e9b3070fe10a98d7d398245364ab81f9bcbe7438fc46bc97a31e68c941 |