Formatted output in OpenOffice/Microsoft Word with Python
Asked Answered
Z

7

7

I am working on a project (in Python) that needs formatted, editable output. Since the end-user isn't going to be technically proficient, the output needs to be in a word processor editable format. The formatting is complex (bullet points, paragraphs, bold face, etc).

Is there a way to generate such a report using Python? I feel like there should be a way to do this using Microsoft Word/OpenOffice templates and Python, but I can't find anything advanced enough to get good formatting. Any suggestions?

Zealotry answered 28/5, 2009 at 13:49 Comment(0)
C
3

A little known, and slightly evil fact: If you create an HTML file, and stick a .doc extension on it, Word will open it as a Word document, and most users will be none the wiser.

Except maybe a very technical person will say, my this is a small Word file! :)

Cirone answered 28/5, 2009 at 17:18 Comment(1)
Then it becomes a normal Word file.Cirone
C
3

Use the Python Docx module for this - 100% Python, tables, images, document properties, headings, paragraphs, and more.

Chenopod answered 27/1, 2011 at 16:53 Comment(0)
U
2

" The formatting is complex(bullet points, paragraphs, bold face, etc), "

Use RST.

It's trivial to produce, since it's plain text.

It's trivial to edit, since it's plain text with a few extra characters to provide structural information.

It formats nicely using a bunch of tools.

Uglify answered 28/5, 2009 at 15:59 Comment(0)
D
1

I know there is an odtwriter for docutils. You could generate your output as reStructuredText and feed it to odtwriter or look into what odtwriter is using on the backend to generate the ODT and use that.

(I'd probably go with generating rst output and then hacking odtwriter to output the stuff I want (and contribute the fixes back to the project), because that's probably a whole lot easier that trying to render your stuff to ODT directly.)

Dropsonde answered 28/5, 2009 at 13:58 Comment(0)
B
1

I've used xlwt to create Excel documents using python, but I haven't needed to write word files yet. I've found this package, OOoPy, but I haven't used it.

Also you might want to try outputting html files and having the users open them in Word.

Boaster answered 28/5, 2009 at 13:59 Comment(0)
P
1

You can use QTextDocument, QTextCursor and QTextDocumentWriter in PyQt4. A simple example to show how to write to an odt file:

>>>from pyqt4 import QtGui
# Create a document object
>>>doc = QtGui.QTextDocument()
# Create a cursor pointing to the beginning of the document
>>>cursor = QtGui.QTextCursor(doc)
# Insert some text
>>>cursor.insertText('Hello world')
# Create a writer to save the document
>>>writer = QtGui.QTextDocumentWriter()
>>>writer.supportedDocumentFormats()
[PyQt4.QtCore.QByteArray(b'HTML'), PyQt4.QtCore.QByteArray(b'ODF'), PyQt4.QtCore.QByteArray(b'plaintext')]
>>>odf_format = writer.supportedDocumentFormats()[1]
>>>writer.setFormat(odf_format)
>>>writer.setFileName('hello_world.odt')
>>>writer.write(doc) # Return True if successful
True

QTextCursor also can insert tables, frames, blocks, images. More information at: http://qt-project.org/doc/qt-4.8/qtextcursor.html

As a bonus, you also can print to a pdf file by using QPrinter.

Patinated answered 4/10, 2012 at 14:56 Comment(0)
M
0

I think OpenOffice has some Python bindings - you should be able to write OO macros in Python.

But I would use HTML instead - Word and OO.org are rather good at editing it and you can write it from Python easily (although Word saves a lot of mess which could complicate parsing it by your Python app).

Ma answered 28/5, 2009 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.