问题
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