Hi,
I have made a connection to libreoffice and want to do some operation through my python script when save menu option is clicked. However I am not able to get callback . I have tried adding event listener, action listener. But didn’t succeeded.
Please help where I am doing wrong.
class UnoObjs:
def __init__(self):
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext)
initialObject = resolver.resolve(
"uno:socket,host=localhost,port=2002;urp;"
"StarOffice.ServiceManager")
self.ctx = initialObject.getPropertyValue("DefaultContext")
self.smgr = self.ctx.ServiceManager
self.desktop = self.smgr.createInstanceWithContext(
"com.sun.star.frame.Desktop", self.ctx)
self.frame = self.desktop.getCurrentFrame()
if not self.frame:
raise Exception("Could not get frame")
self.dispatcher = self.smgr.createInstanceWithContext(
"com.sun.star.frame.DispatchHelper", self.ctx)
self.transformer = self.smgr.createInstanceWithContext(
"com.sun.star.util.URLTransformer", self.ctx)
self.configProvider = self.smgr.createInstanceWithContext(
"com.sun.star.configuration.ConfigurationProvider", self.ctx)
dobj=UiDocument(self.desktop)
#doc = self.desktop.CurrentComponent
#doc.addDocumentEventListener(dobj)
oDoc = self.desktop.getDocument()
oDoc.addDocumentEventListener(dobj)
class UiDocument(unohelper.Base, AdapterPattern):
def __init__(self, desktop):
#self.doc = desktop.CurrentComponent
#self.row = 0 # uncomment for Calc documents only
#self.listen() # Start monitoring doc. events
@property
def Filename(self) -> str:
sys_filename = uno.fileUrlToSystemPath(self.doc.URL)
return os.path.basename(sys_filename)
def listen(self, *args): # OnLoad/OnNew at the earliest
""" Start doc. events monitoring """
self.doc.addDocumentEventListener(self)
def sleep(self, *args): # OnUnload at the latest (optional)
""" Stop doc. events monitoring """
self.doc.removeDocumentEventListener(self)
def documentEventOccured(self, event: DocumentEvent):
""" Intercepts all doc. events """
#self.setCell(event.Source, event.EventName) # only for Calc docs
print("test")
def disposing(self, event: EventObject):
""" Release all activities """
self.sleep()
if __name__ == '__main__':
edit_file_path = "/desktop/test.xls"
office_path = 'soffice'
calc = '-o'
pipe = "--accept=socket,host=localhost,port=2002;urp"
subprocess.Popen([office_path, calc, pipe, edit_file_path])
time.sleep(2)
UnoObjs()