Python macro: insert document also appends title

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

Assumptions? yes! But it would be easier to help, if you show the whole code.

In my test, not appended title to title in main document. Please, show us you all code.

Thanks, I updated my first post

Hallo
It is difficult to create a test environment (with several documents involved) without knowing the functional structure of the main document and the documents to be imported.

The repeated? ( or worse recursive?? ) call of SearchRegex looks strange!

If the fields Title and Subject from the imported stuff are going to influence the original content of all this fields … you need to remove them before the …insertDocumentFromURL(… , …)

For further help you should attach at least 2 Documents (reduced to minimal functual structure) as …odt-documents

Indeed, my imported documents can have themselves some {includetext:.*} beacons that need to be filled with other documents. That’s why I have to use a while loop, but there is no recursivity though.

I was thinking about a simple solution, that would be storing some document properties at the beginning of my script (I know I can retrieve it with ActiveDocument.getDocumentProperties()), then restore it at the very end, but I haven’t found any way to set these properties. Is it possible to change, eg the title of my document from my macro ?

but… I can’t see the problem that you described. If I use the method insertDocumentFromURL the title main not change. Show us a screenshot before and after of execute your code.