How to obtain the currently selected image.Through Python scripting

When the selected content contains multiple paragraphs and images, I cannot find the images by traversing the Selection object.
selection = XSCRIPTCONTEXT.getDocument().getCurrentController().Selection

It depends on the anchor type of the image. The most typical case is when the image is at-char anchored. If you have a text selection, then you can use createEnumeration() to enumerate the paragraphs in the selected text, and then inside that, another createEnumeration() to enumerate all text portions of a single paragraph. All returned portions have a TextPortionType property that tells you if that portion is text, image/frame or something else.

For at-para anchored objects, it’s similar, but there is a separate enumeration for that on the paragraph, see:

Thank you for your reply. I tried the method you provided to iterate through each part element of the paragraph. For images, the TextPortionType is Frame, but I cannot find the image object from the TextPortion object. I want to do some other things with the images, such as changing their size.

You can always check how the ODT export uses the UNO API to write the images. See the code here:

So you can do a .createContentEnumeration(“com.sun.star.text.TextContent”) on the text portion object, which will give you an enumeration of typically a single image, and there you can set e.g. the Size property.

@vmiklos I have tried it, perfect. Thank you