Document automation (Office) using Django - Henk Vos

Tags: django, pun

(Presentations at the Dutch Django/web meeting)

The basic idea: generating microsoft Word documents using data you already have, like a user’s name. A user must be able to easily create a document.

The traditional desktop way: use COM automation to set up the word document. But in a web app you can’t get this to work. The browser runs in a security sandbox: you cannot call into a client-side app like microsoft office. On the server you probably run linux, so you don’t have microsoft word there on the server.

Perhaps you can use open office’s pyuno bridge? Generate content in a rich web editor and generate a PDF? All very hard to get done. Then he saw python-docx. A .docx word document is really xml, packaged in a certain way (zipped, actually). So… cannot we generate the xml with a Django template? Yes, we can!

Those django template variables you want in your xml file? Well, just type them in, curly braces and all, in word or openoffice. Unzip the .docx on the server and run the right .xml file through the Django template engine, zip it up again and you’re done. logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):