Macro: export ODM to ODT

Hello everyone,

I’m trying to write a Python macro to export a .odm file into a “flat” .odt file, without links to external references. This is what I’ve written so far:

import os
import sys

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

def ExportMasterToOdt():
    model = XSCRIPTCONTEXT.getDocument()

    # Update links and delete sections
    model.updateLinks()
    sections = model.Links.Sections
    for name in sections:
        sections[name].dispose()

    args = (PropertyValue(Name="FilterName", Value="writer8"),)
    model.storeToURL(model.getURL().replace(".odm", ".odt"), args)


if __name__ == "__main__":
    ExportMasterToOdt()


g_exportedScripts = (ExportMasterToOdt,)

It works, but problem is: the .odt that is created with this macro still has some .odm “features”. For example, most tracking options are unavailable. It is quite frustrating because if I do the same steps as my script on the LO UI (delete sections, then File → Export), the exported file is a proper odt.

I’ve found a workaround by exporting the odm with the soffice --convert-to odt command line, but I really wish I could do this entirely from the Python macro.

Thank you for reading!

So use »subprocess.run«:

import uno
from subprocess import run
from pathlib import Path

def odm2odt(*_):

    document = XSCRIPTCONTEXT.getDocument()
    filepath = Path( uno.fileUrlToSystemPath( document.URL ))

    run( ['soffice',
         '--convert-to', 'odt',
         '--outdir', f"{filepath.parent}",
         f"{filepath}"] )
1 Like

Thank you! So as far as you know there is no extra PropertyValue I could add to my storeToURL function to meet my needs ?

then Dispatch .uno:SaveAs or .uno:ExportTo
see Apache OpenOffice Community Forum - [Solved] Macro similar to "OpenDocument" & "SaveAs" - (View topic)