How to get a list of Libreoffice.odg objects with a python Macro

Working with Linux (Ubuntu20.04) I’ve got a P&ID made with LibreOffice Draw (.odg file).
There are a lot of objects (each 1text+1symbol grouped together) in the .odg file, each containing the attributes: title,description.

My macro for to list text,title,description of all these objects starts with command:

soffice --invisible --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"

then with python3.8 the .odg file opens and I get the drawpages as follows:

import uno
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
                "com.sun.star.bridge.UnoUrlResolver", localContext )
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
fileURL = uno.systemPathToFileUrl('/home/simkiss/testfile.odg')
model = desktop.loadComponentFromURL(fileURL, "_blank", 0, ())
pages = model.getDrawPages()
for apage in pages:
    pass 	# I realy don't know ???

`desktop.terminate()`

How can I get a list containing text,title,description of the objects.
For me without any java-knowledge it’s hard to understand libreoffice uno concept/documentation and there are less python macro examples.
Farther question: is it possible to change this attributes via python macro?

[Edit - Opaque] Use preformatted text for code
[Edit - Jim K] Fixed code formatting and spellchecked.

def iterate_draw_shapes():
    oDoc = XSCRIPTCONTEXT.getDocument()
    oDrawPages = oDoc.getDrawPages()
    for oDrawPage in oDrawPages:
        for oShape in oDrawPage:
            #mri(oShape)
            message = (
                "title={}\n"
                "description={}\n"
                "text={}\n"
                .format(oShape.Title, oShape.Description, oShape.String))
            if oShape.Title == "title 1":
                oShape.Title = "title 2"
            print(message)

Tips for writing Python-UNO code:

  • Use an introspection tool such as MRI to discover which attributes are available.

  • The APSO extension makes it easier to manage and run python macros.

  • The code above uses XSCRIPTCONTEXT which is available when running the code from the LibreOffice menu rather than connecting to an instance listening on a socket. In that case, output from print() will not show up, so in order to display results, create a message box or write to a file.

2 Likes

alternatively:
– raise an exception with that text. It will be part of an error message box.
– or start the office from a terminal where all print messages will appear.

When I klick on the downloaded mri-1-1-4.oxt file the extension manager starts and shows following message:

(com.sun.star.lang.WrappedTargetRuntimeException){{{Message=“Bad Zip File, ZipException: Zip END signature not found!”,
Context=(com.sun.star.uno.XInterface) @7fd5a4002630(ImplementationName=“com.sun.star.packages.comp.ZipPackage”)}},
TargetException=(any){(com.sun.star.packages.zip.ZipOExeption){{{{Message=“Bad Zip File, ZipException: Zip END signature not found!”,Context=(com.sun.star.uno.XInterface) @7fd5a4002630(ImplementationName=“com.sun.star.packages.comp.ZipPackage”)
On your Extensions site I can find the APSO Extension but I can not find a possibility to download it even when I’m logged in.
An idea how to repaire my extension manger and where to get the APSO .oxt

Download 1.3.4 from Releases · hanya/MRI · GitHub.

Not sure why that would be. Here is what the APSO extension page looks like in my browser, with a nice big button that says “Download”:

You can also get 1.3.0 at Releases · Jean-Marc Zambon / apso · GitLab.

If your LibreOffice user profile gets corrupted then it may prevent extensions from installing. In that case, reset it by deleting the profile folder and restarting LibreOffice. More detailed instructions can be found by searching online.

I don’t have an Extensions site. :slight_smile: People on this forum are your peers.

2 Likes

very much thanks for your help

This is the file to download: https://github.com/hanya/MRI/releases/download/v1.3.4/MRI-1.3.4.oxt

You can’t do development without that.

1 Like

please not this way … Python is not Basic!

…
        message = (f"title= {oShape.Title}\n"
                   f"description= {oShape.Description}\n"
                   f"text= '{oShape.String}'\n")
…

There are so many possible ways of string handling in python 3, and here is yet another I haven’t seen before. I was always happy with %-formatting but now that seems to be considered out of date. What I really wanted to do when writing the question was use Java’s StringBuilder but that’s not needed in Python. Also for the record, I don’t really know how this would be done in Basic - I only learned LO Basic in order to post answers on forums.

Anyway, f-strings do look good, but they won’t work on AOO on Windows (because python 2). Nor do they work on my currently installed version of python (3.5.3). For me, these are significant compatibility problems, and that’s a bigger issue than trying to use the latest style.

but they won’t work on AOO on Windows (because python 2)

worse enough, but is there any AOO for Linux with python3 available?

Nor do they work on my currently installed version of python (3.5.3). For me, these are significant compatibility problems, and that’s a bigger issue than trying to use the latest style.

yes…but the …format() method on Strings and also the old style %-formating works on python2 and python3.5.

Okay, str.format() works on AOO, so I edited the question to use that style.