首页 文章

libreoffice - 运行(python)宏以从Gnu / Linux命令行插入交叉引用

提问于
浏览
2

我已经验证我可以在办公室内运行普通办公室和python宏,但我仍然没有想出如何从命令行运行一个(甚至是hello world one) .

我用Google搜索并查看了其他答案,但我还不完全清楚如何从命令行运行一个开放的办公室宏:

https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=8232建议使用:

office writer.odt "macro://Standard.Module1.Macro1()"

我也看到了:

office "macro://Standard.Module1.Macro1()" writer.odt

无论哪种方式,只需打开文档,既不运行宏也不报告错误 .

How to call an existing LibreOffice python macro from a python script建议运行办公室监听端口并通过它进行通信 .

如果我能做到这一点,我仍然需要找到解释如何插入锚点的API文档(根据我的其他问题asciidoc: is there a way to create an anchor that will be visible in libreoffice writer?

我正在使用RHEL7作为上下文 .

update

oowriter "foo.odt" macro:///Standard.Module1.addXref

适用于办公室基本宏 . 我还没弄明白蟒蛇一个 .

一个问题是我找不到任何调试信息 . 是否有任何日志文件?

另一个问题是使用哪个版本的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配对 . 最好是上面两个中的任何一个(作者4.3.7和python 2.7?或者作者5.1和python 3.5) .

update2

使用与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"

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

1 回答

  • 0

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

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

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

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

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

    您建议的链接显示了如何从不同的Python脚本调用Python宏,这比我们需要的更复杂 . 而是将连接代码(以 localContext = uno.getComponentContext() 开头)和宏代码放在同一个脚本中 . 有关脚本应该包含的内容的示例,请参阅"First play with the Python shell to get familiar" at http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-office-tasks-with-python-macros.html .

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

    此列表是从How do I check for broken internal links in Star Basic?复制的 . 在您的另一个问题中,您还询问了是否检查了断开的链接,所以希望这个问题很有帮助 .

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

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

    EDIT

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

    有关调试信息,可以在Basic IDE中设置检查点 . 对于python,我使用logging module . OpenOffice也有各种日志文件,但我通常不会发现它们有用 .

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

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

相关问题