Merge multiple documents using Master Document programmatic…
Merge multiple documents using Master Document programmatic…
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?
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.
Re: Programmatically load and merge multiple documents
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
viewtopic.php?f=21&t=78814
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Programmatically load and merge multiple documents
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.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
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
Re: Programmatically load and merge multiple documents
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)
Re: Merge multiple documents using Master Document programma
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…
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.
-
- Posts: 145
- Joined: Mon Jun 13, 2016 10:50 am
Re: Merge multiple documents using Master Document programma
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 :
Regards.
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
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
Re: Merge multiple documents using Master Document programma
Thank you Hubert, that’s quite interesting. Do you refer to “Sections” as described here?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.
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.
-
- Posts: 145
- Joined: Mon Jun 13, 2016 10:50 am
Re: Merge multiple documents using Master Document programma
Yes, this is it._savage wrote:Do you refer to “Sections” as described here?
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._savage wrote:However, I am curious how these linked sections handle different document layout and page dimensions when documents are loaded into them.
A linked section works exactly the same way.
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._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.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
Re: Merge multiple documents using Master Document programma
You mean that the styling and formatting of the imported document overrides the styling and formatting of the Master Document?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.
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).
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).hubert lambert wrote:I never tried this, but it sounds like a very bad idea. Convert the docx to the odf format […]
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
-
- Posts: 145
- Joined: Mon Jun 13, 2016 10:50 am
Re: Merge multiple documents using Master Document programma
No, on the contrary : master document styles override subdocument styles... when they share the same name._savage wrote:You mean that the styling and formatting of the imported document overrides the styling and formatting of the Master Document?
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
Re: Merge multiple documents using Master Document programma
Bummer That’s not what I want… thanks for clarifying that!hubert lambert wrote:No, on the contrary : master document styles override subdocument styles...
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.
-
- Posts: 145
- Joined: Mon Jun 13, 2016 10:50 am
Re: Merge multiple documents using Master Document programma
Default styles can't be deleted.
And, as already said, direct formatting is always kept.
And, as already said, direct formatting is always kept.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems