[Solved] storeToURL with Contents of Master Document

The Application Programming Interface and the OASIS Open Document Format

[Solved] storeToURL with Contents of Master Document

Postby gareththered » Fri Feb 24, 2017 5:05 pm

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   Expand viewCollapse view
# 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
gareththered
 
Posts: 2
Joined: Fri Feb 24, 2017 4:47 pm

Re: storeToURL with Contents of Master Document

Postby hubert lambert » Sat Feb 25, 2017 3:07 pm

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

[SOLVED] storeToURL with Contents of Master Document

Postby gareththered » Sat Feb 25, 2017 9:55 pm

Thanks @hubert - that did the trick.

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

Code: Select all   Expand viewCollapse view
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
gareththered
 
Posts: 2
Joined: Fri Feb 24, 2017 4:47 pm


Return to UNO API and ODF

Who is online

Users browsing this forum: No registered users and 2 guests