I have verified that I can run both normal office and python macros from within office but I still haven't figured out how to run one (even the hello world one) from the command line.
I have googled and looked at other answers here but I'm still not entirely clear how to run an open office macro from the command line:
https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=8232 suggests to use:
office writer.odt "macro://Standard.Module1.Macro1()"
I have also seen:
office "macro://Standard.Module1.Macro1()"
writer.odt
Either way around this just opens the document and and neither runs a macro nor reports an error.
Whereas How to call an existing LibreOffice python macro from a python script suggests to run office listening on a port and communicate via that.
If I can get that far I still need to find API documentation that will explain how to insert an anchor (as per my other question asciidoc: is there a way to create an anchor that will be visible in libreoffice writer?)
I'm using RHEL7 for context.
update
oowriter "foo.odt" macro:///Standard.Module1.addXref
works with a office basic macro. I still haven't figured out the python one.
One issue is I can't find any debug information to look at. Are there any log files anywhere?
Another issue is which version of python to use. The RHEL package installs site packages for 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 includes 3.5 with the distro:
>/opt/libreoffice5.1/program/python --version
Python 3.5.0
So to start with I am looking for a hello world python example that pairs a known version of python with a known version of office. Preferably either of the two above (writer 4.3.7 & python 2.7? or writer 5.1 & python 3.5).
update2
Using python3.5 as installed with office5.1 I have a working hello world using the following:
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 )
This works with either version of open office via:
/usr/bin/oowriter --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"
So the final part is to add the cross reference.