我已驗證我可以在辦公室內運行普通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)
這通過可與開放式辦公室的任一版本。
oowriter「foo.odt」macro:///Standard.Module1.addXref與Office基本宏一起使用。 –
「coool」腳本鏈接到(http://stackoverflow.com/questions/37611030/how-do-i-check-for-broken-internal-links-in-star-basic那裏)很有趣,但不指出要使用哪個版本的python。它不是2.7.5而不是3.x –
爲了讓'coool'腳本工作,我下載了代碼,將所有'configparse'內容註釋掉了,並且更新它以使用python 3庫。我花了大約1個小時。 –