2

我已经验证我可以在 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"

所以最后一部分是添加交叉引用。

4

1 回答 1

1

看起来该命令需要一个额外的斜杠。这在 Ubuntu 上对我有用:

lowriter "Untitled 1.odt" macro:///Standard.Module1.SayHello

Module1在名为under的模块中调用此方法My Macros & Dialogs / Standard

Sub SayHello
    MsgBox("Hello, World!")
End Sub

上述方法仅适用于基本宏。对于 Python 宏,标准命令行方法是连接到 Office 的侦听实例。警告:这将比在 Office 中运行慢很多(可能是 10 倍)。

您建议的链接显示了如何从不同的 Python 脚本调用 Python 宏,这比我们在这里需要的要复杂。相反,将连接代码(以 开头localContext = uno.getComponentContext())和宏代码放在同一个脚本中。有关脚本中应包含的内容的示例,请参阅http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-上的“首先熟悉 Python shell” office-tasks-with-python-macros.html

至于创建锚点,LibreOffice 中有许多不同的对象可以用作锚点:

此列表复制自如何检查 Star Basic 中损坏的内部链接?. 在您的另一个问题中,您还询问了有关检查断开链接的问题,因此希望该问题对您有所帮助。

创建超链接的一种方法是编辑某些文本的HyperLinkURL属性。例如,假设有一个名为 的书签MyBookmark。然后下面的代码将当前选中的文本变为超链接:

viewcursor = currentController.getViewCursor()
viewcursor.HyperLinkURL = "#MyBookmark"

编辑

关于使用哪个版本的python,目前LibreOffice使用python 3,OpenOffice使用python 2。

有关调试信息,您可以在 Basic IDE 中设置检查点。对于 python,我使用logging 模块。OpenOffice 也有各种日志文件,但我通常觉得它们没有帮助。

关于python的问题,您是否尝试过我发布的链接?如果是这样,你走了多远?

我认为您不会找到很多 RHEL 示例。首先尝试让它在像 Ubuntu 这样的桌面发行版上运行,然后将该方法应用于 RHEL。

于 2016-07-23T01:08:55.073 回答