I've written this LibreOffice Python macro, that prints (in ASPO terminal) the inner code of each "formula" (i.e. LibreOffice Math element included in LibreOffice Writer content) of my LibreOffice Writer document.
How can I modify it to instead get the same result only for the formulas in the current selection? I was able to get all shapes in a page, but how can I tell if a given shape is within the current selection?
I'm using LibreOffice 24.2.7.2 on Linux Mint 22.2.
import uno
def explore_objects_in_selection():
doc = XSCRIPTCONTEXT.getDocument()
selection = doc.getCurrentSelection()
# Useless variable in this code for now, equivalent to
#selection = doc.CurrentController.getSelection()
shapes = []
for draw_page in doc.getDrawPages():
# Maybe testing here if shape is in selection, but how?
for shape in draw_page:
shapes.append(shape)
for shape in shapes:
#if hasattr(shape, 'ShapeType'):
# In my doc, shape.ShapeType or shape.getShapeType()
# is 'FrameShape'
assert hasattr(shape.getEmbeddedObject(), 'Formula')
# Inner code of each formula in my doc
print(shape.getEmbeddedObject().Formula)
As I said above, for now, even if I select only a part of the Writer doc, all formulas are printed. My goal is to print only the ones included in the selected area.