Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a Word document using Python? [closed]

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.

like image 264
Huuuze Avatar asked Jun 23 '09 20:06

Huuuze


People also ask

How do I insert a python file into Word?

Open the target document in Microsoft Word and place the cursor where the source code will appear. Select Insert. In the Text group, select Object. In the Object dialog box, select the Create New tab.


1 Answers

A couple ways you can create Word documents using Python:

  • Use COM automation to create a document using the MS Word object model (using pywin32). http://python.net/crew/pirx/spam7/
  • Automate OpenOffice using Python: http://wiki.services.openoffice.org/wiki/Python
  • If rtf format is OK, use the PyRTF library: http://pyrtf.sourceforge.net/

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).

like image 191
codeape Avatar answered Sep 20 '22 21:09

codeape