使用uno与openoffice.org/libreoffice交互
unotools的Python项目详细描述
unotools允许您使用 “UNO桥”。其目的是使处理opendocument比 使用原始的uno/pyuno编写脚本。
unools非常简单,但是您应该理解unoapi。
还有其他工具。
如何安装
要求
- openoffice.org/libreoffice 3.4或lator
- python 3.3或lator
在ubuntu 14.04
安装libreoffice、uno library和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 112014, 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
在Python3.3的情况下,需要单分派包。确认python libreoffice中包含的解释器版本。
$ hg clone ssh://hg@bitbucket.org/ambv/singledispatch
设置pythonpath以解析libreoffice的python中的其他包 翻译。
$ exportPYTHONPATH="/path/to/singledispatch/:/path/to/unotools/"
在mac os x上,soffice和python命令如下。
$ /Applications/LibreOffice.app/Contents/MacOS/soffice --version
LibreOffice 4.4.0.3 de093506bcdc5fafd9023ee680b8c60e3e0645d7
确认导入unools包:
$ /Applications/LibreOffice.app/Contents/MacOS/python Python 3.3.5 (default, Jan 222015, 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
看一下这些示例脚本,它将帮助您如何使用unools。
逐步与口译员互动
通过与uno交互文档,可以学习如何使用unools。
启动libreoffice:
$ soffice --accept='socket,host=localhost,port=8100;urp;StarOffice.Service'
>>>fromunotoolsimportSocket,connect>>>fromunotools.component.writerimportWriter>>>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.Textobjectat0x1064a8e10>
作者继承 XTextRange 接口并具有这些方法。为了充分利用unools,您必须 了解unoapi。
有一个提示来确认组件中存在哪些方法。这个 _show_attributes()是卸载组件的帮助程序方法 继承。
>>>text._show_attributes()[...'getElementType','getEnd','getImplementationId','getImplementationName','getPropertySetInfo','getPropertyValue','getSomething','getStart','getString','getSupportedServiceNames','getText','getTypes',...]
尽管这些方法是camelcase,但您也可以调用python样式 方法。
>>>text.getString()'Hello\nWorld\n'>>>text.get_string()'Hello\nWorld\n'
两者都是用于转换python的相同方法和unools组件句柄 样式为原始方法。
让我们将此文档另存为odt。
>>>fromunotools.unohelperimportconvert_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转换示例脚本
- 将自述文件更新为教程
0.3.2(2014-06-07)
- 为Calc添加了一些类
- 命令行参数(-d, –datadirs)可以存储多个目录
0.3.1(2014-05-07)
- 添加了命令行参数(-f,–file)以便能够读取 打开文档文件
0.3.0(2014-05-05)
- 首次发布