使用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')

然后,helloworld被放入文档窗口。

>>>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)

  • 首次发布

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
regex Java split(),使用包含特定字符的整个单词作为分隔符   java无法启动发现(Android附近连接API v2)   多线程Java:producerconsumer两个线程停止工作,冻结   java XDocReport:生成的docx文档错误“Word在尝试打开文件时遇到错误”   安卓 java。ClassCastException:java。util。无法将HashMap强制转换为com。保罗。ImapNotes。杂项。OneNote   找不到java CassandraSQLContext密钥   java重命名Derby模式   java是否可以扩展使用生成器模式和私有构造函数的类?   java如何获取Hotmail验证码图像   如何用Java在项目文件夹中创建文件?   java JOptionPane取消按钮和获取输入   变量如何打开equals语句以接受Java中的多个参数?   Ping url并获取java中的状态   tokenize是否有任何基于Java的智能单词标记器可以对句子中的相邻单词进行标记?   java如何从纹理下删除上面的纹理?   java递归除以3   弗莱希指数问题(Java)   Java正则表达式模式匹配在第二次出现时不起作用