Date: prev next · Thread: first prev next last
2015 Archives by date, by thread · List index


I am trying to program a Global DocumentEventListener in Python.
Then on a certain event (e.g. File Open), I want to analyse/print the paragraph structure of the 
document.

I am running LibreOffice on port 2002. I am using the internal Python from LibreOffice, but I run 
the script externally, from a Terminal.

This is on Mac OS X 10.10, and LO 4.4.2.2.

The listener works and gives output until I start an Enumeration, for example enumerating the 
paragraphs. After the first element LO hangs completely, and the only way out is to kill it. I have 
tried also with enumerationg the TextFields in the document and it also hangs.

So I am wondering if I do something illegal or unsupported, or that I have hit a bug.

LO is started with:
/Applications/LibreOffice.app/Contents/MacOS/soffice 
--accept='socket,host=localhost,port=2002;urp;StarOffice.Servicemanager' --nologo --nodefault

Here is the relevant code. During the processing I disable the listener to prevent some recursive 
calling, but in fact that doesn't make a difference.

import uno
import unohelper

from com.sun.star.document import XDocumentEventListener
import PYUNO.DemoBase as DemoBase 

listener = None
globalBroadcaster =  None

class MyEventListener(XDocumentEventListener, unohelper.Base):
    # XDocumentEventListener
    def documentEventOccured(self, event):
        globalBroadcaster.removeDocumentEventListener(self)
        print("Event name: {}".format(event.EventName))
        if event.EventName == "OnLoadFinished":
            doc = event.Source
            print("Document: Title: {} Location: {}".format(doc.Title, doc.Location))
            showportions(doc)
        globalBroadcaster.addDocumentEventListener(self)

    # parent-interface XDocumentEventListener
    def disposing(self, event):
        pass

def showportions(doc):
    if doc.supportsService("com.sun.star.text.TextDocument"):
        parenum = doc.Text.createEnumeration()
        while parenum.hasMoreElements():
            para = parenum.nextElement()
            print("get para: {}".format(para.String))

and in main (after setting up the connection):

    global globalBroadcaster
    context = XSCRIPTCONTEXT.getComponentContext()
    globalBroadcaster = context.getValueByName(
        '/singletons/com.sun.star.frame.theGlobalEventBroadcaster')
    listener = MyEventListener()
    globalBroadcaster.addDocumentEventListener(listener)

And here is the output when I open a Writer document:

Event name: OnPrepareViewClosing
Event name: OnPrepareUnload
Event name: OnViewClosed
Event name: OnUnload
Event name: OnUnfocus
Event name: OnFocus
Event name: OnLoadFinished
Document: Title: Letter.odt Location: file:///Users/piet/TEST/LibreOffice/Letter.odt
get para: To:

The first paragraph gets printed, then it hangs.
-- 
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.