Hello everyone !
My problem is quite simple. My OO Writer document has a few beacons that look like {bookmark:NAME}
. For each beacon found in the doc, I want to create a bookmark with the name NAME at the beacon’s position, then erase the beacon.
Here is my closest solution:
import re
def Finalize():
ActiveDocument = XSCRIPTCONTEXT.getDocument()
ImportDocuments(ActiveDocument)
# Create bookmarks
searchBookmarks = ActiveDocument.createSearchDescriptor()
searchBookmarks.SearchString = "\{bookmark:.*\}"
searchBookmarks.SearchRegularExpression = True
searchBookmarks.SearchAll = True
searchBookmarks.SearchCaseSensitive = True
searchBookmarks.SearchWords = False
bookmarksFound = ActiveDocument.findAll(searchBookmarks)
for bookmarkIndex in range(0, bookmarksFound.getCount()):
bookmarkFound = bookmarksFound.getByIndex(bookmarkIndex)
bookmarkName = re.sub(r'{bookmark:(.*)}', r'\1', bookmarkFound.getString())
bookmark = ActiveDocument.createInstance("com.sun.star.text.Bookmark")
bookmark.setName(bookmarkName)
ActiveDocument.Text.insertTextContent(bookmarkFound, bookmark, True)
def ImportDocument(ActiveDocument):
pathname = os.path.dirname(ActiveDocument.getURL())
importCursor = ActiveDocument.Text.createTextCursor()
search = ActiveDocument.createSearchDescriptor()
search.SearchString = "\{includetext:.*\}"
search.SearchRegularExpression = True
search.SearchAll = True
search.SearchCaseSensitive = True
search.SearchWords = False
selsFound = ActiveDocument.findAll(search)
while selsFound.getCount() > 0:
for selIndex in range(0, selsFound.getCount()):
selFound = selsFound.getByIndex(selIndex)
importCursor.gotoRange(selFound, False)
filename = re.sub(r'{includetext:(.*)}', r'\1', selFound.getString())
filename = filename.replace("${path}", pathname)
importCursor.insertDocumentFromURL(filename, ())
selsFound = SearchRegex(ActiveDocument, "\{includetext:.*\}")
importCursor.gotoStart(False)
The problem is I really struggle with XTextRange type and cursors, LO throws a RuntimeException saying “text interface and cursor not related” at the insertTextContent call. Do you have any idea ?
Thanks a lot