跳转到主要内容

使用UNO与OpenOffice.org/LibreOffice交互

项目描述

UnoTools允许您通过“UNO桥接”与OpenOffice.org/LibreOffice交互。目的是使处理OpenDocument比使用原始UNO/PyUNO进行脚本化更容易。

unotools非常简单,但您应该了解UNO API。

还有其他工具。

如何安装

要求

  • OpenOffice.org/LibreOffice 3.4或更高版本

  • Python 3.3或更高版本

在Ubuntu 14.04上

安装libreoffice、uno库和python3

$ sudo aptitude install -y libreoffice libreoffice-script-provider-python uno-libs3 python3-uno python3

我喜欢使用virtualenvwrapper来创建临时环境

$ sudo aptitude install -y virtualenvwrapper
$ mkvirtualenv -p /usr/bin/python3.4 --system-site-packages tmp3

确认导入uno模块

(tmp3)$ python
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import uno

从PyPI安装unotools

(tmp3)$ pip install unotools

在Mac OS X上

https://www.libreoffice.org/下载LibreOffice DMG软件包并安装。

$ hg clone ssh://hg@bitbucket.org/t2y/unotools

在Python 3.3的情况下,需要singledispatch软件包。确认LibreOffice中包含Python解释器版本。

$ hg clone ssh://hg@bitbucket.org/ambv/singledispatch

设置PYTHONPATH以在LibreOffice的Python解释器中解决额外的包。

$ export PYTHONPATH="/path/to/singledispatch/:/path/to/unotools/"

在Mac OS X上,soffice和python命令如下。

$ /Applications/LibreOffice.app/Contents/MacOS/soffice --version
LibreOffice 4.4.0.3 de093506bcdc5fafd9023ee680b8c60e3e0645d7

确认导入unotools包

$ /Applications/LibreOffice.app/Contents/MacOS/python
Python 3.3.5 (default, Jan 22 2015, 17:12:45)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import unotools

如何使用(在Ubuntu 14.04上)

启动libreoffice

(tmp3)$ soffice --accept='socket,host=localhost,port=8100;urp;StarOffice.Service'

https://bitbucket.org/t2y/unotools/raw/default/sample-scripts下载示例脚本

(tmp3)$ python sample-scripts/writer-sample1.py -s localhost
(tmp3)$ python sample-scripts/calc-sample1.py -s localhost -d sample-scripts/datadir/
(tmp3)$ ls
sample-calc.html  sample-calc.pdf  sample-calc_html_eaf26d01.png
sample-scripts  sample-writer.html  sample-writer.pdf
sample.csv  sample.doc  sample.ods  sample.odt  sample.xls

有一个将odt/ods转换为pdf的示例脚本。

(tmp3)$ python sample-scripts/pdf-convert-sample1.py -s localhost -f sample.odt
(tmp3)$ python sample-scripts/pdf-convert-sample1.py -s localhost -f sample.ods
(tmp3)$ ls sample.pdf

查看这些示例脚本,然后它将帮助您使用unotools。

逐步与解释器交互

使用UNO与文档交互,让你学会如何使用unotools。

启动LibreOffice

$ soffice --accept='socket,host=localhost,port=8100;urp;StarOffice.Service'
>>> from unotools import Socket, connect
>>> from unotools.component.writer import Writer
>>> context = connect(Socket('localhost', 8100))
>>> writer = Writer(context)

现在,你可以在LibreOffice中看到新的文档窗口。

>>> writer.set_string_to_end('Hello\n')
>>> writer.set_string_to_end('World\n')

然后,将《Hello》和《World》放入文档窗口。

>>> text = writer.text
>>> text
<unotools.component.writer.Text object at 0x1064a8e10>

Writer继承XTextRange接口并具有这些方法。为了充分利用unotools,你必须理解UNO API。

有一个技巧可以确认组件中存在哪些方法。_show_attributes()是一个unotools组件继承的辅助方法。

>>> text._show_attributes()
[ ...
 'getElementType',
 'getEnd',
 'getImplementationId',
 'getImplementationName',
 'getPropertySetInfo',
 'getPropertyValue',
 'getSomething',
 'getStart',
 'getString',
 'getSupportedServiceNames',
 'getText',
 'getTypes',
  ...
]

尽管这些方法是大驼峰命名法,但你也可以调用Python风格的命名方法。

>>> text.getString()
'Hello\nWorld\n'

>>> text.get_string()
'Hello\nWorld\n'

这两个方法相同,unotools组件将Python风格的方法转换为原始方法。

让我们将此文档保存为odt

>>> from unotools.unohelper import convert_path_to_url
>>> url = convert_path_to_url('./test1.odt')
>>> url
'file:///Users/t2y/work/repo/unotools/test1.odt'
>>> writer.store_to_url(url, 'FilterName', 'writer8')
>>> writer.close(True)

如果你想读取文件系统上的OpenDocument,就像这样。

>>> writer = Writer(context, convert_path_to_url('./test1.odt'))

更改日志

0.3.3 (2015-03-21)

  • 添加了PDF转换示例脚本

  • 更新了README作为教程

0.3.2 (2014-06-07)

  • 为calc添加了一些类

  • 将命令行参数(-d, –datadirs)的数据目录选项更改为能够存储多个目录

0.3.1 (2014-05-07)

  • 添加了命令行参数(-f, –file)以能够读取OpenDocument文件

0.3.0 (2014-05-05)

  • 第一个版本

项目详情


下载文件

下载适合您平台的应用程序。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源分布

unotools-0.3.3.tar.gz (17.3 kB 查看哈希值)

上传时间

由以下提供支持