• This repository has been archived on 16/Apr/2022
  • Stars
    star
    274
  • Rank 150,274 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 12 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Mail merge for Office Open XML (docx) files without the need for Microsoft Office Word.

docx Mail Merge

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.

Project Status

I wrote the library some years ago to scratch an itch I was having. The itch is long gone and therefore the need to keep this library maintained. Over years I’ve merged some PRs, but my interest in maintaining this library has faded. I have decided to archive this repository. There is a community effort to continue work on this codebase, which can be found here: https://pypi.org/project/docx-mailmerge2/.

Installation

Installation with pip:

$ pip install docx-mailmerge

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.

More Repositories

1

SwiftInitializerGenerator

Xcode Source Code Extension to Generate Swift Initializers
Swift
717
star
2

HAP

Swift implementation of the Homekit Accessory Protocol
Swift
361
star
3

NetService

Swift NetService (Bonjour / Zeroconf / mDNS) implementation for Linux
Swift
117
star
4

DNS

Swift implementation of DNS Records / RR
Swift
63
star
5

SRP

Secure Remote Password (SRP) for Swift
Swift
53
star
6

Lark

Swift SOAP Client
Swift
53
star
7

Glob

Glob for Swift 5
Swift
25
star
8

django-federated-login

Django Federated Login provides an authentication bridge between Django projects and OpenID-enabled identity providers.
Python
19
star
9

django-airbrake

Django Airbrake provides a logging handler to push exceptions and other errors to airbrakeapp or other airbrake-compatible exception handler services (e.g. aTech Media's Codebase).
Python
17
star
10

INI

Swift INI parser based on (NS)Scanner
Swift
16
star
11

Swift-PriorityQueue

Binary heap implementation in Swift
Swift
15
star
12

pathfinding

A* pathfinding algorithm in Python using OSM data
Python
8
star
13

HKDF

HMAC-based Extract-and-Expand Key Derivation Function (HKDF) in Swift
Swift
7
star
14

My-Home

My DIY Homekit setup in Swift
Swift
7
star
15

Swift-Security

Proof of concept for accessing Keychain using Swift
Swift
6
star
16

NetService-Example

Swift
5
star
17

mvvm

Proof of concept Model-View-ViewModel implementation in Python
Python
4
star
18

Bignum

Swift big number library
Swift
4
star
19

CLibSodium

Swift overlay for libsodium
Swift
3
star
20

ncurses-example

Swift
3
star
21

SPA941-Google-Contact-Sync

PHP
2
star
22

Lark-Example

Example of Lark SOAP library written in Swift
Swift
2
star
23

hc-icy

ICE / Essent e-thermostaat client in Go
Go
1
star
24

spa-phone-api

Provides an API for Linksys/Sipura VoIP SPA Phones
Python
1
star
25

Cdns_sd

Swift C modulemap for dns_sd.h
Swift
1
star
26

hc-my-homekit

My DIY HomeKit / HomeControl accessories in Go
Go
1
star