Create and Call Uno Service in Python

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Create and Call Uno Service in Python

Post by saleem145 »

Hello,

I have an Addin and an Addon. Addin is in C++ and the Addon is python.

In the addon I am trying the following code,

service = CreateUnoService("my_module.MyService1")
service.Initialize(a,b,c,d)

(The initialize routine is defined in service, and can be called succressfully from basic)

It doesn't work. The second line tanks....I am not good at python so I do not know how to print errors....

try:
service.Initialize(a,b,c,d)
except Exception as inst:
f = open('/tmp/err.log', 'w')
f.write('saleem\n')
f.write(type(inst))

It does create the file and put my name in it, but the next line does not do anything.....

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Create and Call Uno Service in Python

Post by Villeroy »

Code: Select all

class Office:
    '''Frequently used methods in office context'''
    def __init__(self, ctx=uno.getComponentContext()):
        self.ctx = ctx
        self.smgr = self.ctx.ServiceManager
        
    def createUnoService(self, service):
        return self.smgr.createInstance(service)

    def getDesktop(self):
        return self.smgr.createInstanceWithContext("com.sun.star.frame.Desktop",self.ctx)

    def getCurrentComponent(self):
        return self.getDesktop().getCurrentComponent()

    def getCurrentFrame(self):
        return self.getDesktop().getCurrentFrame()

    def getCurrentComponentWindow(self):
        return self.getCurrentFrame().getComponentWindow()

    def getCurrentContainerWindow(self):
        return self.getCurrentFrame().getContainerWindow()

    def getCurrentController(self):
        return self.getCurrentFrame().getController()

    def callMRI(self,obj=None):
        '''Create an instance of MRI inspector and inspect the given object (default is selection)'''
        if not obj:
            obj = self.getCurrentController().getSelection()
        mri = self.createUnoService("mytools.Mri")
        mri.inspect(obj)
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Jan_J
Posts: 187
Joined: Wed Apr 29, 2009 1:42 pm
Location: Poland

Re: Create and Call Uno Service in Python

Post by Jan_J »

It does create the file and put my name in it, but the next line does not do anything.....
The second line should save type (i.e. name of class) of your exception. Make sure that the file is closed properly after writing. Either

Code: Select all

f = open(.....)
f.write(...)
f.write(...)
f.close()
or (even better)

Code: Select all

with open(.....) as f:
    f.write(...)
    f.write(...)
Besides, you are trying to write to the file something that is not a string... (type is not a string). Try rather

Code: Select all

f.write(repr(type(inst)))
JJ ∙ https://forum.openoffice.org/pl/
LO (25.2|24.8) ∙ Python (3.12|3.10) ∙ Unicode 16 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Create and Call Uno Service in Python

Post by saleem145 »

service = CreateUnoService("my_module.MyService1")
service.Initialize(a,b,c,d)

The exception throw is of type --

exceptions.AttributeError

Any idea what that means?? What else could I add to the logging to shed further light on whats going wrong??

Thanks,

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Create and Call Uno Service in Python

Post by Villeroy »

How did you define def CreateUnoService?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply