I need to crunch a Calc spreadsheet composed of many individual sheets. The Wiki has a detailed Python guide that I am following. I installed the libreoffice-script-provider-python package and am able to import the uno package without error. However, every time the create_instance function is invoked the interpreter crashes with a segmentation fault. A small example is below:
import uno
CTX = uno.getComponentContext()
SM = CTX.getServiceManager()
def create_instance(name, with_context=False):
if with_context:
instance = SM.createInstanceWithContext(name, CTX)
else:
instance = SM.createInstance(name)
return instance
path = uno.systemPathToFileUrl('/home/user/temp/test.ods')
desktop = create_instance('com.sun.star.frame.Desktop', True)
doc = desktop.loadComponentFromURL(path, '_default', 0, ())
How can I open a Calc spreadsheet with uno?
Setup:
- OS: Ubuntu 22.04
- Python: 3.10.12
- LibreOffice: 7.3.7
Update: The log of an even smaller example. Launching Calc beforehand does not really change the outcome, but is added for completion.
$ soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"
$ python3
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import uno
>>> CTX = uno.getComponentContext()
>>> SM = CTX.getServiceManager()
>>> name='com.sun.star.frame.Desktop'
>>> instance = SM.createInstanceWithContext(name, CTX)
Segmentation fault (core dumped)
Okay, I used your setup (Ubuntu 22.04, Python 3.10, LO 7.3.7), and indeed, the code listed in the question does produce a segmentation fault.
But it looks like you didn't even try the tutorial even though your comments suggest otherwise. The resolver is required to connect to the running instance of LibreOffice, as shown in "First play with the Python shell to get familiar."
In contrast, the code linked in the question wasn't intended to be used with that setup. It works when run from within LibreOffice as a macro, where
XSCRIPTCONTEXTis available.