[Solved] storeToURL with Contents of Master Document

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
gareththered
Posts: 2
Joined: Fri Feb 24, 2017 4:47 pm

[Solved] storeToURL with Contents of Master Document

Post by gareththered »

I've a Python script which opens a Master Document and updates it, so that I can save the document in various formats.

The issue I have is that while saving as a PDF or an MS Word (docx) saves fine as a standalone document, saving as a Writer (odt) still saves with links. That means that the output document cannot (for example) be emailed to anyone as they won't have access to the original linked sub-documents.

The Navigator pane (F5) has a Save Content as well button, but I can't figure out how to do this with UNO.

Snippets of Python I'm using are:

Code: Select all

# Start a headless instance of libreoffice, then:

import uno
from com.sun.star.beans import PropertyValue

localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
context = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
svcmgr = context.ServiceManager
desktop = svcmgr.createInstanceWithContext("com.sun.star.frame.Desktop", context)

model = desktop.loadComponentFromURL('file:///home/gareth/Master.odm', "_blank", 0, ())
model.updateLinks()

model.text.getString() # so that I can see it's updated

args = (PropertyValue('FilterName',0,'MS Word 2007 XML',0),)
model.storeToURL('file:///home/gareth/test.docx',args)

args = (PropertyValue('FilterName',0,'writer_pdf_Export',0),)
model.storeToURL('file:///home/gareth/test.pdf',args)

# This next one is the problem:
args = (PropertyValue('FilterName',0,'writer8',0),)
model.storeToURL('file:///home/gareth/test.odt',args)
test.odt still has links to the original sub-documents.

Does anyone know the code required to force contents to be saved in the file instead of the link?
Last edited by Hagar Delest on Sat Feb 25, 2017 9:58 pm, edited 1 time in total.
Reason: tagged [Solved].
LibreOffice 5.3.0.5 on Windows 10 / LibreOffice 5.1.2 on Ubuntu 16.04
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: storeToURL with Contents of Master Document

Post by hubert lambert »

Hello,

As a very first export, you may do this :
1. call dispose() on all sections of your master document (this will remove all the links);
2. save the master document AS a new odt document (storeAsURL method to leave the master doc untouched).

Then you can proceed with further exports the way you did.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
gareththered
Posts: 2
Joined: Fri Feb 24, 2017 4:47 pm

[SOLVED] storeToURL with Contents of Master Document

Post by gareththered »

Thanks @hubert - that did the trick.

Just for the benefit of anyone who arrives here, the code that does this is:

Code: Select all

sections = model.Links.Sections
for name in sections:
    sections[name].dispose()
Both storeToURL as well as storeAsURL seems to work after running the above.
LibreOffice 5.3.0.5 on Windows 10 / LibreOffice 5.1.2 on Ubuntu 16.04
Post Reply