Hi everyone,
I’m writing a Python macro for a Writer document, and I’m using the insertDocumentFromURL
function several times to import other documents in mine. But I just realized that at each function call, the imported file “Title” property is appended to my main document’s Title. Same thing for the Subject property. And I’m using my document title as a Field in my document content, so it’s quite annoying…
Moreover, I guess it could be the case for other properties that I haven’t seen yet.
Here is my code:
from com.sun.star.beans import PropertyValue
import os
import re
def ImportDocument(ActiveDocument):
pathname = os.path.dirname(ActiveDocument.getURL())
importCursor = ActiveDocument.Text.createTextCursor()
selsFound = SearchRegex(ActiveDocument, "\{includetext:.*\}")
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)
prop = PropertyValue()
prop.Name = "DocumentTitle"
prop.Value = ""
importCursor.insertDocumentFromURL(filename, (prop,))
selsFound = SearchRegex(ActiveDocument, "\{includetext:.*\}")
def SearchRegex(ActiveDocument, regex):
search = ActiveDocument.createSearchDescriptor()
search.SearchString = regex
search.SearchRegularExpression = True
search.SearchAll = True
search.SearchCaseSensitive = True
search.SearchWords = False
return ActiveDocument.findAll(search)
NB: I’m working with .docx documents only, not .odt.
Do you have any ideas ?
Thanks a lot