Insert several images at once using macro scripting in LibreOffice

本小妞迷上赌 提交于 2019-12-12 02:13:58

问题


I am writing a macro for LibreOffice Writer in Python. I need to insert several images in one document, one after another with minimal space inbetween them.

The folowing code inserts all the images in the same area and all of them are overlapped.

I need to advance the cursor below the inserted image everytime a new image is inserted. I have tried the cursor.gotoEnd(), cursor.goDown() and other such methods but none seem to work.

How do I make this work?

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

回答1:


Insert a paragraph break after each image:

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

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

This will separate the images... by a paragraph

Andrew's book is a basic source for solving many OpenOffice scripting problems: +1



来源:https://stackoverflow.com/questions/31449712/insert-several-images-at-once-using-macro-scripting-in-libreoffice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!