News for hackers. Stuff that matters.
Re:I made a native Python module for MS Word docx by nailer on Wednesday December 30, 2009 @06:52AM (#1022299)
Hi Hacker News,

I was recently wandering around StackOverflow and PyPI and wondering why the only solutions to make MS Office documents from Python seemed to be based around either COM automation, OpenOffice automation, or calling Java or .net libraries.

So I made this module, which reads and writes Microsoft Office Word 2007 docx files. These are referred to as 'WordML', 'Office Open XML' and 'Open XML' by Microsoft.

They can be opened in Microsoft Office 2007, Microsoft Mac Office 2008, OpenOffice.org 2.2, and Apple iWork 08.

The docx module has the following features:

Making documents

• Paragraphs

• Bullets

• Numbered lists

• Multiple levels of headings

• Tables

Editing documents

Thanks to lxml module, we can:

• Search and replace

• Extract plain text of document

• Add and delete items anywhere within the document

• Run xpath queries against particular locations in the document - useful for retrieving data from user-completed templates.

It's only a couple of hundred lines - lxml does most of the heavy work - but it's incredibly simple to use, check out example.py in the link.

Hope you find it useful.

Mike

Re:I made a native Python module for MS Word docx by bigsassy on Wednesday December 30, 2009 @09:36AM (#1022452)
Thank you, nailer! I've been putting up with Word randomly crashing when using it's COM object in long running processes. Now I can skip Word altogether and work on the file level. THANKS!
Re:I made a native Python module for MS Word docx by marcinw on Wednesday December 30, 2009 @12:40PM (#1022770)
Thanks Mike for providing such a useful module!

Some plans I have to add to this module (if you don't plan too already):

Add support for:

* Pass a font-size, bold, italic, bold+italic and spacing to paragraph method to modify font

* Pass a table row header to table method

* Specify table column widths

* Change text alignment for table rows/headers, etc.

* Change text style in table cells

* Ability to change background color for every other table row

* Create method to insert a page/section break

* Create a method to add an image at a specific position in document page

On one other note:

I'm not 100% positive, but thumbnail.jpg/thumbnail.wmf may result in inadvertent disclosure of sensitive data if using this to generate report documents...

Re:I made a native Python module for MS Word docx by nailer on Wednesday December 30, 2009 @01:25PM (#1022892)
Awesome! I welcome contributions. Could you send me something for page & section breaks?

I'm already working on images and 100% nose coverage. I'm intending to do document properties after that.

Re: zebra striping for tables, we already have that via the inbuilt styles. But more options to control styles would be useful.

Coding style is:

* Functional

* Google style - http://code.google.com/p/soc/wiki/PythonStyleGuide

* Unit tests are handled with nose / coverage

Re:I made a native Python module for MS Word docx by coderdude on Wednesday December 30, 2009 @08:33AM (#1022376)
This is great! I'm already using lxml for my project and using ReportLab to create pdf reports of data I'm mining from the Web. This will be perfect for me and now I can generate Microsoft Word reports.
Re:I made a native Python module for MS Word docx by nailer on Wednesday December 30, 2009 @09:47AM (#1022470)
Glad you like it. A great deal of the credit belongs to the lxml authors (Stephan Behnel in particular). Though this only took a day or do, I've been using lxml for projects for years and always loved it - in particular, the xpath makes modifying and querying trees really easy.

If more people used LXML and saw how easy it was, the whole idea of modifying serialized XML 'Cthulu style' would be seen as being as odd as modifying other data by manipulating their pickled forms.

Re:I made a native Python module for MS Word docx by omouse on Wednesday December 30, 2009 @03:52PM (#1023199)
Which license is it under? GPL I hope?
Re:I made a native Python module for MS Word docx by nailer on Wednesday December 30, 2009 @04:34PM (#1023271)
MIT
Re:I made a native Python module for MS Word docx by gaius on Thursday December 31, 2009 @08:34AM (#1024249)
Cheers dude :-)