When walking the paragraphs of a Writer document, how do I find and access properties of the images as I walk the paragraphs? (Doing this in Python, but any example would help

Hm... I looked into XRay and MRI (with a nice thread on using introspection here) but I can't quite figure out how this works from Python when I parse a document.Mr.Dandy wrote:Images are localised on DrawPage object. Use XRay or MRI to explore your document and find this property.
Code: Select all
...
context = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
document = desktop.loadComponentFromURL("file://path/to/file.doc", "_blank", 0, ())
def get_uno_attr(obj, attr) :
"""Helper function that reads an attribute from a UNO object; returns the attribute, or None if it didn't exist."""
try : return getattr(obj, attr)
except AttributeError : return None
pages = document.getDrawPage()
penum = pages.createEnumeration()
while penum.hasMoreElements() :
elem = penum.nextElement()
graphic = get_uno_attr(elem, "Graphic")
if graphic is not None :
print("Found an image: " + str(elem.getSize()) + " " + graphic.MimeType + " " + elem.GraphicURL)
# The following is based on F3KTotal's answer below.
psetinfo = graphic.getPropertySetInfo()
pset = psetinfo.getProperties()
for p in pset :
print(p.Name, p.Type, p.Handle, p.Attributes)
Code: Select all
Sub S_find_Images
odrawpage = Thiscomponent.drawpage
for i = 0 to odrawpage.count - 1
oshape = odrawpage.getbyindex(i)
if oshape.supportsservice("com.sun.star.text.TextGraphicObject") then
msgbox "I'm a graphic shape, my name is: "+oshape.name
aprops = oshape.Propertysetinfo.Properties
for k = 0 to ubound (aProps)
sstring = sstring + "Name: "+aProps(k).Name+" ----Type: "+aProps(k).Type.Name+chr(10)
next k
msgbox sstring
end if
next i
End Sub
Code: Select all
sub TraverseGraphicObjects
dim oDocument as variant: oDocument = ThisComponent
dim oGraphicCollection as variant: oGraphicCollection = oDocument.getGraphicObjects
dim i as integer
dim aGraphic as variant
for i = 0 to oGraphicCollection.count-1
aGraphic = oGraphicCollection(i)
Rem Do something with the graphic
msgbox(aGraphic.Name)
next i
end sub
Code: Select all
if graphic.Type == 1: # GraphicType.PIXEL
with open("image.bmp", "wb") as imgf:
imgf.write(graphic.DIB.value)