Posts Tagged ‘mailmerge’

OpenOffice MailMerge using CLI

Wednesday, March 26th, 2008

“Mail merge” is a term that covers form letters (both paper and email), postcards, address labels, envelopes–any single document that you wish to send to a lot of different people. Once you have a database of contacts you can use this to automate all kinds of tasks, such as sending reminders to customers with overdue invoices, product announcements, Christmas cards, and what have you.

While working on some project i wanted to generate openoffice documents filling data like customer name automatically. Ideally this would ran on webserver and send .odt document as attachement.

PyUNO python framework is extremely powerfull, but work like “COM” in windows and would require running openoffice which is suboptimal on webserver. after searching a bit i found alternative - OOoPy which doesnt require openoffice to be installed on system.

After short playing i come up with odtcli.py python script which enable to assign values in openoffice document via command line - odtcli.py

odtcli requires OOoPy installed. You can download source rpm here - OOoPy source rpm.

Installation instructions:

1) build and install OOoPy rpm

2) download odtcli.py script andrun chmod a+rx odtcli.py

Usage instructions:

Lets say you want to set customer last name in odt document.

1) Locate in document place where you want customer lastname to be shown.

2) In openoffice menu choose “Insert” -> “Fields” -> “Other”

3) In dialog window choose “Variables” tab

4) Choose “Set Variable” option on left side, and at the bottom of dialog enter “customerlastname” as Name, some default value and “Text” as format and hit “Insert” button. Screenshot - Variable Field Creation

5) If this variable shows up more than once in document, for next occurrence use “Show Variable” instead of “Set Variable”

6) Save this document as “form-template.odt”. example here form-template.odt

7) Check that variable is properly created by running

# odtcli.py /tmp/form-template.odt
Variable : customerlastname = Customer

8) now lets say you want to create from template form for customer Mustermann

odtcli.py -s"customerlastname=Mustermann" /tmp/form-template.odt /tmp/form-muste\
rmann.odt

9) check that variable set properly

odtcli.py -l /tmp/form-mustermann.odt
Variable : customerlastname = Mustermann

10) open /tmp/form-mustermann.odt in oowriter and enjoy ;-)

odtcli.py usage

Usage: odtcli.py [OPTIONS] inputfile [outputfile]
 Options:
 -h, --help                    print this help
 -s, --set-var='name=value'    set value of variable
 -l, --list-vars               print variable names and values
Example: set values customerlastname to Mustermann and
     customerfirstname to Hans using template from customer-data.odt
 python odtcli.py --set-var="customerlastname=Mustermann" \
                  --set-var="customerfirstname=Hans" \
                  customer-data.odt /tmp/customer-566.odt