Create openoffice .odt document with Python

北城余情 提交于 2019-12-21 01:19:22

问题


How can I create an open office .odt file from Python?

I'm looking at this http://wiki.openoffice.org/wiki/Python, but am confused. I've already got Python 2.7 so where do I go from here? The above link talks about Open Office shipping with Python. Have I already got it?? And do I even need OpenOffice? Isn't there just some template, some way that a document needs to look like so it will will recognized as odt? Would I need the actual editor?

I'd like something like https://github.com/mikemaccana/python-docx, but for Open Office.

Sorry for this open ended question, but I really have looked around and feel that I am missing some vital link/understanding about what is required.


回答1:


I use relatorio to be able to produce odt. You can have a look at the doc here




回答2:


UNO Python bindings allows to run python scripts inside OpenOffice/LibreOffice, like Basic macroses.

They allows to do almost everything that OpenOffice can do by itself, including creating ODT documents of course, but they required to be run in specific environment and depends on OpenOffice.

Here is basic tutorial for Python-UNO in OpenOffice: http://www.openoffice.org/udk/python/python-bridge.html

To edit ODT documents from Python I found this library: https://joinup.ec.europa.eu/software/odfpy/home but I never tried it actually.

For my task (small modification of existing document) I ended with manual editing of XML files in .odt zip file using zipfile Python module and lxml library for parsing and editing XML.




回答3:


Another posibility is the odfpy module, you can see a short introduction here.

Python API and tools to manipulate OpenDocument files

Odfpy is a library to read and write OpenDocument v. 1.2 files. The main focus has been to prevent the programmer from creating invalid documents. It has checks that raise an exception if the programmer adds an invalid element, adds an attribute unknown to the grammar, forgets to add a required attribute or adds text to an element that doesn’t allow it.

It's on pipy, so you can install it with

pip install odfpy


来源:https://stackoverflow.com/questions/18439396/create-openoffice-odt-document-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!