Open ODS file with UNO and Python

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
luofeiyu
Posts: 53
Joined: Thu Sep 14, 2017 2:11 am

Open ODS file with UNO and Python

Post by luofeiyu »

I want to open the '"/tmp/sample.ods" with uno in python3' console:

Code: Select all

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('/tmp/sample.ods')
desktop = create_instance('com.sun.star.frame.Desktop', True)
doc = desktop.loadComponentFromURL(path, '_default', 0, ())
It encounter issue on the line "desktop = create_instance('com.sun.star.frame.Desktop', True)".

Try another code in the webpage https://christopher5106.github.io/offic ... acros.html
exit.png
exit.png (70.86 KiB) Viewed 11652 times

Code: Select all

import socket  # only needed on win32-OOo3.0.0
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()
refuse.png
refuse.png (72.93 KiB) Viewed 11652 times
LibreOffice 7.4.7.2 on Debian 12
Post Reply