我已经验证我可以在 office 中运行普通的 office 和 python 宏,但我仍然没有弄清楚如何从命令行运行一个(甚至是 hello world )。
我在这里搜索并查看了其他答案,但我仍然不完全清楚如何从命令行运行开放式办公室宏:
https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=8232 建议使用:
office writer.odt "macro://Standard.Module1.Macro1()"
我也看过:
office "macro://Standard.Module1.Macro1()"
作家.odt
不管怎样,这只是打开文档,既不运行宏也不报告错误。
而如何从 python 脚本调用现有的 LibreOffice python 宏 建议在端口上运行 office 并通过该端口进行通信。
如果我能做到这一点,我仍然需要找到解释如何插入锚点的 API 文档(根据我的另一个问题 asciidoc:有没有办法创建一个在 libreoffice writer 中可见的锚点?)
我正在使用 RHEL7 作为上下文。
更新
oowriter "foo.odt" macro:///Standard.Module1.addXref
与办公基本宏一起使用。我还没有弄清楚python之一。
一个问题是我找不到任何要查看的调试信息。任何地方都有日志文件吗?
另一个问题是使用哪个版本的 python。RHEL 包为 python 2.7 安装站点包。
>rpm -q --whatprovides /usr/bin/writer libreoffice-writer-4.3.7.2-5.el7_2.1.x86_64 >rpm -ql libreoffice-pyuno-4.3.7.2-5.el7_2.1 ... /usr/lib64/python2.7/site-packages/uno.py
Libreoffice5.1 包括 3.5 和发行版:
>/opt/libreoffice5.1/program/python --version
Python 3.5.0
因此,首先我正在寻找一个 hello world python 示例,它将已知版本的 python 与已知版本的 office 配对。最好是上述两者之一(writer 4.3.7 & python 2.7?或 writer 5.1 & python 3.5)。
更新2
使用与 office5.1 一起安装的 python3.5 我有一个使用以下内容的工作你好世界:
import uno
# get the uno component context from the PyUNO runtime
localContext = uno.getComponentContext()
# create the UnoUrlResolver
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext )
# connect to the running office
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
# get the central desktop object
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
# access the current writer document
model = desktop.getCurrentComponent()
# access the document's text property
text = model.Text
# create a cursor
cursor = text.createTextCursor()
# insert the text into the document
text.insertString( cursor, "Hello World", 0 )
这适用于任一版本的开放式办公室:
/usr/bin/oowriter --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"
所以最后一部分是添加交叉引用。