How can I create a Word document using Python?

Huuuze picture Huuuze · Jun 23, 2009 · Viewed 77.1k times · Source

I'd like to create a Word document using Python, however, I want to re-use as much of my existing document-creation code as possible. I am currently using an XSLT to generate an HTML file that I programatically convert to a PDF file. However, my client is now requesting that the same document be made available in Word (.doc) format.

So far, I haven't had much luck finding any solutions to this problem. Is anyone aware of an open source library (or *gulp* a proprietary solution) that may help resolve this issue?

NOTE: All possible solutions must run on Linux. I believe this eliminates pywin32.

Answer

codeape picture codeape · Jun 23, 2009

A couple ways you can create Word documents using Python:

EDIT:

Since COM is out of the question, I suggest the following (inspired by @kcrumley's answer):

Using the UNO library to automate Open Office from python, open the HTML file in OOWriter, then save as .doc.

EDIT2:

There is now a pure Python python-docx project that looks nice (I have not used it).