Merge multiple documents using Master Document programmatic…

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Merge multiple documents using Master Document programmatic…

Post by _savage »

Using the UI, I can play through the scenario as follows: menu “File → New → Master Document”, then in the Navigator window click “Insert” and select a number of documents. The documents are then loaded into the Master in order of their selection (it seems). Then I use the menu “File → Export” and save the whole thing as .ODT. (See also this thread and few other How-Tos on the interwebs.)

Now I would like to do the same programmatically, but I can’t quite figure out how to get to the “Master Document”. Is it a predefined URL argument to loadComponentFromURL() like e.g. private:factory/swriter?

And once the Master is open, I would load and append multiple documents using loadComponentFromURL() again, using the Master’s frame name? (Which is what, then?)

Once all documents are loaded into the Master, can I then grab a paragraph enumeration of the entire document and noodle over all collected paragraphs and their text portions as outlined in this old thread?
Last edited by _savage on Fri Mar 23, 2018 2:46 am, edited 1 time in total.
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Programmatically load and merge multiple documents

Post by JeJe »

Don't know about master documents but you can open a series of documents and use gettransferable to merge them by copying them in turn to a new file. See:

viewtopic.php?f=21&t=78814
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: Programmatically load and merge multiple documents

Post by _savage »

JeJe wrote:[…] you can open a series of documents and use gettransferable to merge them by copying them in turn to a new file. See: viewtopic.php?f=21&t=78814
Thanks JeJe. Does this handle embedded images and tables and everything else that floats around in a document? There’s a comment in the thread mentioning issues with tables.
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Programmatically load and merge multiple documents

Post by JeJe »

I think so, yeah. The problem with tables is with going to the start of the document when a table is there, not with gettransferable, a workaround is given too.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: Merge multiple documents using Master Document programma

Post by _savage »

Kudos to this thread for mentioning private:factory/swriter/GlobalDocument as the URL to open a “Master Document”.

How to populate it, and then access it remains to be seen… :?
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Merge multiple documents using Master Document programma

Post by hubert lambert »

Hello,

As far as I know, a master document is nothing else that a text document with special gui elements to easily manage subdocuments (aka linked sections), indexes, link updates...
So inserting a document into another is "as easy as" creating a section linked to that document, then breaking the link if wanted.
Something like this, in basic :

Code: Select all

sub insertdoc
    dest = "T1.odt"
    source = "T2.odt"
    desturl = convertToURL("/path/to/" & dest)
    dim props(0) as new com.sun.star.beans.PropertyValue
    props(0).Name = "Hidden"
    props(0).Value = True
    doc = stardesktop.loadComponentFromURL(desturl, "_default", 0, props())
    section = doc.createInstance("com.sun.star.text.TextSection")
    section.setName(source)
    dim filelink as new com.sun.star.text.SectionFileLink
    filelink.FileURL = convertToURL("/path/to/" & source)
    section.FileLink = filelink
    T = doc.getText()
    T.insertTextContent(T.getEnd(), section, False)
    T.removeTextContent(section)
    doc.store()
    doc.close(True)
end sub
Regards.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: Merge multiple documents using Master Document programma

Post by _savage »

hubert lambert wrote:As far as I know, a master document is nothing else that a text document with special gui elements to easily manage subdocuments (aka linked sections), indexes, link updates...

So inserting a document into another is "as easy as" creating a section linked to that document, then breaking the link if wanted.
Thank you Hubert, that’s quite interesting. Do you refer to “Sections” as described here?

With some adjusting to Python your proposed code works. However, I am curious how these linked sections handle different document layout and page dimensions when documents are loaded into them. For example, loading just a single DOCX file as a Section into a Master Document looked somewhat crooked compared to loading that same document directly. That may be because the document is now loaded into a Section, thus adjusting to existing document properties?

Cheers.
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Merge multiple documents using Master Document programma

Post by hubert lambert »

_savage wrote:Do you refer to “Sections” as described here?
Yes, this is it.
_savage wrote:However, I am curious how these linked sections handle different document layout and page dimensions when documents are loaded into them.
When you work with master document, it's hardly recommended to used styles : if the subdocument uses a style (of any family) which name exists in the master document, the formatting of the last is used. If the subdocument uses new styles, they are imported in the master document. Direct formatting is always kept.
A linked section works exactly the same way.
_savage wrote:For example, loading just a single DOCX file as a Section into a Master Document looked somewhat crooked compared to loading that same document directly.
I never tried this, but it sounds like a very bad idea. Convert the docx to the odf format before doing anything using LibreOffice. If needed, convert back to docx when you finish the editing stuff.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: Merge multiple documents using Master Document programma

Post by _savage »

hubert lambert wrote:When you work with master document, it's hardly recommended to used styles : if the subdocument uses a style (of any family) which name exists in the master document, the formatting of the last is used. If the subdocument uses new styles, they are imported in the master document. Direct formatting is always kept. A linked section works exactly the same way.
You mean that the styling and formatting of the imported document overrides the styling and formatting of the Master Document?

That would be the preferred option, actually. In fact, I’d be happy to remove all styling/formatting from the Master Document and use only the that of the imported document(s).
hubert lambert wrote:I never tried this, but it sounds like a very bad idea. Convert the docx to the odf format […]
I don’t have much control over the actual file format because it’s clients’ choice. However, in the case of loading files into the Master Document I think HTML will be what I see most (i.e. HTML and CSS styling).
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Merge multiple documents using Master Document programma

Post by hubert lambert »

_savage wrote:You mean that the styling and formatting of the imported document overrides the styling and formatting of the Master Document?
No, on the contrary : master document styles override subdocument styles... when they share the same name.
When the style only exists in the subdocument, then it is imported and therefore used by the master document.
The master document is responsible for keeping coherence of formatting.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: Merge multiple documents using Master Document programma

Post by _savage »

hubert lambert wrote:No, on the contrary : master document styles override subdocument styles...
Bummer :cry: That’s not what I want… thanks for clarifying that!

However, thinking about it: what if I were to remove all style names from the master first? That can be done, I think. Need to do some digging ;)
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Merge multiple documents using Master Document programma

Post by hubert lambert »

Default styles can't be deleted.
And, as already said, direct formatting is always kept.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
Post Reply