Loading a document in OpenOffice using an external Python program

I am trying to create a python program (using pyUNO) to make some changes to the Calc OpenOffice worksheet.

I launched OpenOffice earlier in accept mode to be able to connect from an external program. Apparently it should be as simple as:

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) #The calling it not exactly this way, just to simplify the code DESKTOP.loadComponentFromURL('file.ods') 

But I get an AttributeError when I try to access loadComponentFromURL . If I create dir(DESKTOP) , I only see the following attributes / methods:

 ['ActiveFrame', 'DispatchRecorderSupplier', 'ImplementationId', 'ImplementationName', 'IsPlugged', 'PropertySetInfo', 'SupportedServiceNames', 'SuspendQuickstartVeto', 'Title', 'Types', 'addEventListener', 'addPropertyChangeListener', 'addVetoableChangeListener', 'dispose', 'disposing', 'getImplementationId', 'getImplementationName', 'getPropertySetInfo', 'getPropertyValue', 'getSupportedServiceNames', 'getTypes', 'handle', 'queryInterface', 'removeEventListener', 'removePropertyChangeListener', 'removeVetoableChangeListener', 'setPropertyValue', 'supportsService'] 

I read that there is an error doing the same thing, but in OpenOffice 3.0 (I use OpenOffice 3.1 over Red Hat5.3). I tried using the workaround described here , but they don't seem to work.

Any ideas?

+6
python pyuno
source share
2 answers

It has been a long time since I did something with PyUNO, but looking at the code that worked last time, I ran it in '06, I made my boot document as follows:

 def urlify(path): return uno.systemPathToFileUrl(os.path.realpath(path)) desktop.loadComponentFromURL( urlify(tempfilename), "_blank", 0, ()) 

Your example is a simplified version, and I'm not sure if you deleted the extra arguments intentionally or not intentionally.

If loadComponentFromURL does not exist, then the API has changed or something else is wrong, I read your code and it looks like you are doing the same thing that I have.

I don’t think that the dir () method on the desktop will be useful, since I believe that the __getattr__ method __getattr__ used for the proxy server through requests and all the methods that you printed are utility methods used for the stand-in object for com.sun.star.frame.Desktop .

I think that perhaps the failure may be that there is no method named loadComponentFromURL that has exactly one argument. Providing version 4 of the arguments may lead to the discovery and use of the method. It could just be an impedance mismatch between Python and Java, where Java has a signature method overload.

+4
source share

All Articles