用Python创建openoffice.odt文档

2024-05-17 01:20:55 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从Python创建一个打开的office.odt文件?

我在看这个http://wiki.openoffice.org/wiki/Python,但是我很困惑。我已经有了Python2.7,那我该怎么做呢?上面的链接讨论了使用Python的开放式Office发布。我已经拿到了吗??我还需要OpenOffice吗?难道不是只有一些模板,一个文档需要看起来像这样才能被识别为odt吗?我需要真正的编辑吗?

我想要像https://github.com/mikemaccana/python-docx这样的东西,但要开放式办公室。

很抱歉这个开放式的问题,但我真的环顾四周,觉得我错过了一些重要的环节/理解什么是必需的。


Tags: 文件文档httpsorggithub模板http编辑
3条回答

UNO Python绑定允许在OpenOffice/LibreOffice内部运行Python脚本,就像基本的宏一样。

它们允许OpenOffice自己做几乎所有的事情,当然包括创建ODT文档,但是它们需要在特定的环境中运行,并且依赖于OpenOffice。

以下是OpenOffice中Python UNO的基本教程:http://www.openoffice.org/udk/python/python-bridge.html

为了编辑Python中的ODT文档,我找到了这个库:https://joinup.ec.europa.eu/software/odfpy/home,但实际上我从未尝试过。

对于我的任务(对现有文档的小修改),我使用zipfilePython模块和lxml库对.odt zip文件中的XML文件进行了手动编辑,用于解析和编辑XML。

我使用relatorio来生成odt。你可以看看at the doc here

另一种可能性是odfpy模块,您可以看到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.

它在pipy上,所以你可以用

pip install odfpy

相关问题 更多 >