Search code examples
libreofficelibreoffice-basicopenoffice-impress

LibreOffice how to recup the current element


I want to retrieve the current element in LibreOffice Impress to apply changes to it.

For example, I'm trying to retrieve this shape to change the text in it with macros.

image of rectangle with text "Recup this shape"

I tried to find information with the X-ray tool but without success.


Solution

  • To get the currently selected shape:

    oSel = ThisComponent.getCurrentController.getSelection()
    oShape = oSel.getByIndex(0)
    Print oShape.getString()
    

    To iterate through all shapes in the slide, start with ThisComponent.getDrawPages() and then use XrayTool.

    You may also find the following snippet of python code helpful:

    def iterate_draw_shapes():
        oDrawPage = oDrawPages.getByIndex(1)
        for oShape in oDrawPage:
            if oShape.supportsService("com.sun.star.drawing.TextShape"):
                oTexts = oShape.createEnumeration()
                while oTexts.hasMoreElements():
                    oText = oTexts.nextElement()
                    oTextPortions = oText.createEnumeration()
                    while oTextPortions.hasMoreElements():
                        oTextPortion = oTextPortions.nextElement()