[Chicago] mailmerge for xml?

Cosmin Stejerean cosmin at offbytwo.com
Fri May 1 21:44:46 CEST 2009


On Fri, May 1, 2009 at 1:51 PM, Lukasz Szybalski <szybalski at gmail.com>wrote:

> Hello,
> Sorry if its not too related.....but...
>
> Does anybody know a documentation that would show how to create  a
> openoffice template and then fill in the data from an xml?
>
> 1. I have an existing xml with a structure
> 2. I have an existing openoffice document/form
> 3. I want to fill in information like: Firstname, lastname...etc
> 4. I want to easly move the xml data to the openoffice form.
> 5. I will automate the transfer using python uno later on (unless xslt
> will do it for me)
>
> What I'm looking for is a way to define field names in a form that
> would correspond to a xml. I don't want to translate xml nodes to some
> field name but I would rather do:
>
> xml:
>     <ClientApp>
>           <Org>my company</Org>
>            <Name>Full name of the comapny</Name>
>           <Version>1.02 (4.02)</Version>
>       </ClientApp>
>
> in my openoffice template create a table and fill it in like this:
>
> |   ClientApp.Name       |  ClientApp.Version     |
>
>
> Anybody did anything like that? Its like a mailmerge, but for xml to a
> form, instead of csv to mailing label.
>

We've done similar things at work in the past, although we filled OpenOffice
templates with data from a database. If you're using lxml.objectify to
translate your XML into an object tree and have some flexibility over your
ODT layout I would simply use a regex for the transformation with some code
that looks like

import re

pattern = re.compile('\$\$(.+?)\$\$')

for match in pattern.findall(otdbody):
  current = root # object form lxml.objectify
  for level in match.split('.'):
     current = getattr(current, level)
  odtbody.replace(match, current)


I think that should do it.

-- 
Cosmin Stejerean
http://offbytwo.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20090501/6768ff6d/attachment.htm>


More information about the Chicago mailing list