views:

32

answers:

1

Hello,

I am writing a django application and there is something I don't know how to do. Say you have a database with users and several .doc files you might want to send to those users (postal letters, not electronicaly). I am wondering if there is a way to automatically create those letters from templates using my user database to fill in some markers? (i.e. name, address, etc.)

I've heard of the "pod" python library but I've never used it. The documentation seems to say to use openoffice templates, but mine are in MS Word format. Since they are often updated by people who only use MS Office, I'm stuck.

Any help would be appreciated.

J.

+2  A: 

There are many different ways to do this, depending on the context. Here are some ideas, ranked in decreasing order of difficulty.

  • Microsoft Word includes a facility for this, called Mail Merge. You can control Word from Python using COM hooks, by installing pywin32. This would do exactly the same as invoking Mail Merge by opening up Microsoft Word. This would have to be done on a Windows computer with Office installed, probably not your server. See http://bytes.com/topic/python/answers/165364-ms-word-mail-merge-automation, or Google "python mail merge".

  • OpenOffice uses Python as a macro language (I believe) and exposes an API using Python-UNO. You could make OpenOffice convert the files in to its format, and then use pod to perform the mail merge.

  • You could tell your users to give you data in a better format, such as OpenOffice templates.

  • You could export the users' data from Django, give it to the people writing the templates, and tell them to do their own mail merge.

Whatever you choose, this will not be easy.

katrielalex
Since it will be postal letters and that the only things I will have to replace in the template will be names and addresses, I think I am going to ask for templates in .pdf format and then overwrite those pdf with names and addresses at the right spot. Do you think this would be easier to achieve? I've never used ReportLab either.
@jlibioul: no, that would be significantly harder; PDF modification is very difficult. I still think that it would be easiest to export the users' data and give it to the people writing the templates, who can then perform a mail merge or whatever they want to do.
katrielalex