Merge multiple documents using Master Document programmatic…

Keyboard macros or custom scripts

Merge multiple documents using Master Document programmatic…

Postby _savage » Fri Mar 23, 2018 1:07 am

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.11 using LO 5.3.6.1, Gentoo Linux using LO 5.3.4.2 headless.
_savage
 
Posts: 162
Joined: Sun Apr 21, 2013 12:55 am

Re: Programmatically load and merge multiple documents

Postby JeJe » Fri Mar 23, 2018 1:23 am

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:

https://forum.openoffice.org/en/forum/v ... 21&t=78814
Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 232
Joined: Wed Mar 09, 2016 2:40 pm

Re: Programmatically load and merge multiple documents

Postby _savage » Fri Mar 23, 2018 1:27 am

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: https://forum.openoffice.org/en/forum/v ... 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.11 using LO 5.3.6.1, Gentoo Linux using LO 5.3.4.2 headless.
_savage
 
Posts: 162
Joined: Sun Apr 21, 2013 12:55 am

Re: Programmatically load and merge multiple documents

Postby JeJe » Fri Mar 23, 2018 1:43 am

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.
Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 232
Joined: Wed Mar 09, 2016 2:40 pm

Re: Merge multiple documents using Master Document programma

Postby _savage » Mon Apr 02, 2018 1:25 am

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.11 using LO 5.3.6.1, Gentoo Linux using LO 5.3.4.2 headless.
_savage
 
Posts: 162
Joined: Sun Apr 21, 2013 12:55 am

Re: Merge multiple documents using Master Document programma

Postby hubert lambert » Mon Apr 16, 2018 10:01 pm

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   Expand viewCollapse view
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
hubert lambert
 
Posts: 95
Joined: Mon Jun 13, 2016 10:50 am

Re: Merge multiple documents using Master Document programma

Postby _savage » Fri Apr 20, 2018 11:20 am

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.11 using LO 5.3.6.1, Gentoo Linux using LO 5.3.4.2 headless.
_savage
 
Posts: 162
Joined: Sun Apr 21, 2013 12:55 am

Re: Merge multiple documents using Master Document programma

Postby hubert lambert » Fri Apr 20, 2018 12:47 pm

_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
hubert lambert
 
Posts: 95
Joined: Mon Jun 13, 2016 10:50 am

Re: Merge multiple documents using Master Document programma

Postby _savage » Fri Apr 20, 2018 1:06 pm

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.11 using LO 5.3.6.1, Gentoo Linux using LO 5.3.4.2 headless.
_savage
 
Posts: 162
Joined: Sun Apr 21, 2013 12:55 am

Re: Merge multiple documents using Master Document programma

Postby hubert lambert » Fri Apr 20, 2018 1:49 pm

_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
hubert lambert
 
Posts: 95
Joined: Mon Jun 13, 2016 10:50 am

Re: Merge multiple documents using Master Document programma

Postby _savage » Fri Apr 20, 2018 1:52 pm

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.11 using LO 5.3.6.1, Gentoo Linux using LO 5.3.4.2 headless.
_savage
 
Posts: 162
Joined: Sun Apr 21, 2013 12:55 am

Re: Merge multiple documents using Master Document programma

Postby hubert lambert » Fri Apr 20, 2018 1:59 pm

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
hubert lambert
 
Posts: 95
Joined: Mon Jun 13, 2016 10:50 am


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 3 guests