1

我已驗證我可以在辦公室內運行普通office和python宏,但我仍然沒有想出如何運行一個(偶數來自命令行的hello world one)。libreoffice - 運行(python)宏從Gnu/Linux命令行插入交叉引用

我用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上下文。

更新

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蟒蛇的例子,對蟒蛇的已知版本的已知版本辦公室。上述兩者中的任何一個(作者4.3.7 & python 2.7或作者5.1 & python 3.5)。

UPDATE2

使用python3.5與office5安裝。1我有一個工作的Hello World使用以下:

/usr/bin/oowriter --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"

所以最後一部分是添加交叉引用:

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) 

這通過可與開放式辦公室的任一版本。

回答

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()開始)和宏代碼放在同一個腳本中。有關腳本內容的示例,請參閱http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-office-tasks-with-python-macros.html上的「首先使用Python shell熟悉」。

至於創建錨,有許多在LibreOffice的不同的對象,可以作爲錨發揮作用的:

此列表複製自How do I check for broken internal links in Star Basic?。在你的另一個問題中,你也詢問了檢查是否存在斷開的鏈接,所以希望這個問題很有幫助。

創建超鏈接的一種方法是編輯某些文本的HyperLinkURL屬性。例如,假設有一個名爲MyBookmark的書籤。然後將以下代碼改變了當前選擇的文本轉換成一個超鏈接:

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

EDIT

關於蟒使用的版本,目前的LibreOffice使用Python 3和OpenOffice使用Python 2.

對於調試信息,您可以在Basic IDE中設置檢查點。對於python,我使用logging module。 OpenOffice也有各種日誌文件,但我通常沒有發現它們有幫助。

關於python的問題,你嘗試了我發佈的鏈接嗎?如果是這樣,你有多遠?

我不認爲你會發現很多RHEL的例子。試着讓它在像Ubuntu這樣的桌面發行版上工作,然後將這種方法適應於RHEL。

+0

oowriter「foo.odt」macro:///Standard.Module1.addXref與Office基本宏一起使用。 –

+0

「coool」腳本鏈接到(http://stackoverflow.com/questions/37611030/how-do-i-check-for-broken-internal-links-in-star-basic那裏)很有趣,但不指出要使用哪個版本的python。它不是2.7.5而不是3.x –

+0

爲了讓'coool'腳本工作,我下載了代碼,將所有'configparse'內容註釋掉了,並且更新它以使用python 3庫。我花了大約1個小時。 –