在 LibreOffice 中使用宏脚本一次插入多个图像

Insert several images at once using macro scripting in LibreOffice

我正在 Python 中为 LibreOffice Writer 编写宏。 我需要在一个文档中插入几张图片,一张接一张,中间只有最小的 space。

以下代码将所有图像插入同一区域,并且所有图像都重叠。

每次插入新图像时,我都需要将光标移到插入图像下方。 我已经尝试了 cursor.gotoEnd()、cursor.goDown() 和其他此类方法,但 none 似乎有效。

我该如何进行这项工作?

def InsertAll():
    desktop = XSCRIPTCONTEXT.getDesktop()
    doc=desktop.loadComponentFromURL('private:factory/swriter','_blank',0,())

    text = doc.getText()
    cursor = text.createTextCursor()

    file_list = glob.glob('/path/of/your/dir/*.png')
    for f in file_list:
        img = doc.createInstance('com.sun.star.text.TextGraphicObject') 
        img.GraphicURL = 'file://' + f
        text.insertTextContent(cursor, img, False)
        cursor.gotoEnd(False) <- doesnt advance the cursor downwards

    return None

在每张图片后插入一个段落分隔符:

from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK

    text.insertTextContent(cursor, img, False)
    text.insertControlCharacter(cursor, PARAGRAPH_BREAK, False)
    cursor.gotoEnd(False)

这将用一段分隔图像...

A​​ndrew 的书是解决许多 OpenOffice 脚本问题的基本资源:+1