Home > ODT-JinjaMerge

ODT-JinjaMerge

ODT-JinjaMerge is a project mainly written in Python, it's free.

Use the Jinja Template System to perform openoffice mailmerges on the commandline

Overview

This is an early proof of concept. Currently this is VERY hardcoded, but I thought it would be worth releasing: The merge-csv.py file requires a file called 'data.csv'

If you're comfortable with python and the commandline you should be able to find your way around.

Preparation:

The script expects a 'data.csv' file and 'content4.xml.tmpl' file in the same directory

You will need to install Jinja::

easy_install Jinja2

About the content4.xml.tmpl file

This file is typically created as follows:

  1. Create an OpenOffice Document and include Jinja style syntax for merge fields

  2. Unpack the document using an unzip program

  3. Name the content.xml file content4.xml.tmpl

  4. Get a suitable data.csv file with fields that match the content4.xml.tmpl

  5. run the merge-csv.py command to execute the merge

  6. put the merged output (content.xml file) back into a copy of the unpacked openoffice document

  7. zip it up again (rename to odt if necessary)

Usage

While in the directory with the 'data.csv' and 'content4.xml.tmpl' run the following::

python merge-csv.py > content.xml

The result will be an ODT (openoffice style) content.xml file

This can be added to an unzipped openoffice document (ODT) to replace the content.xml, then rezip and rename to ODT. The resulting document will contain your merged output.

Utilities

In additon to merge-csv.py, there is also prettyprint.py which takes a content.xml file and outputs an easier to read version. Pretty print is most oftent used on files generated by OpenOffice since they tend to save on white space at the expense of readability. Still very badly coded, it takes no input and expects to be in the same directory as the content.xml file.

usage::

python prettyprint.py > cleanedupversion.xml

Typical Project

Create a file and include fields like::

{{user['First_Name']}} {{user['Last_Name']}} {{user['Sector_of_Interest']}} {{user['Profile_Resume']}}

These would be derived from the data.csv and then merged into the template.

Create directory for the project::

mkdir myproject mv myodtfile.odt myproject

mkdir myproject/source cd myproject/source unzip ../myodtfile.odt

The result will be a source directory containing the openoffice file structure, including a content.xml file

Run prettyprint on the content.xml::

path/to/prettyprint.py content.xml > prettycontent.xml
cp prettycontent.xml content.xml

Then edit the content.xml to make any customizations.

Finally follow the instructions at the top to create a new merge of the odt.

Previous:cookbooks