Search code examples
pythonexport-to-exceluno

Libre office uno api save as xls


As far i have following code to force an ods to recalc all values in spred sheet and save it that into several formats.

But i can not find the filter for XLS. Has anyone an idea how to find it out?

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

def _toProperties(**args):
    props = []
    for key in args:
        prop = PropertyValue()
        prop.Name = key
        prop.Value = args[key]
        props.append(prop)
    return tuple(props)

# start first
# libreoffice --headless --accept="socket,host=0,port=8001,tcpNoDelay=1;urp"    

inputFile = 'file:///home/user/Downloads/in.ods'
outputFile = 'file:///home/user/Downloads/out.xls'

# import the OpenOffice component context
local = uno.getComponentContext()
# access the UnoUrlResolver service - this will allow to connect to OpenOffice.org program
resolver = local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local)
# load the context and you are now connected - you can access OpenOffice via its API mechanism
context = resolver.resolve("uno:socket,host=localhost,port=8001;urp;StarOffice.ServiceManager")
remoteContext = context.getPropertyValue("DefaultContext")

# service responsible for the current document called desktop
desktop = context.createInstanceWithContext("com.sun.star.frame.Desktop", remoteContext)
document = desktop.getCurrentComponent()

# load, calculateAll(), save
document = desktop.loadComponentFromURL(inputFile, "_blank", 0, ())
document.calculateAll()

# ods
# document.storeAsURL(outputFile, ())

# pds
#document.storeToURL(outputFile, _toProperties(FilterName="calc_pdf_Export"))

# csv
#document.storeToURL(outputFile, _toProperties(FilterName="Text - txt - csv (StarCalc)"))

# xls
document.storeToURL(outputFile, _toProperties(FilterName="calc_MS_Excel_40"))

# xlsx
#document.storeToURL(outputFile, _toProperties(FilterName="Calc Office Open XML"))

document.dispose()

Solution

  • Use the following basic macro to get a list of all available filter names

    ' DannyB Tue Oct 28, 2003 9:49 am
    ' http://www.oooforum.org/forum/viewtopic.phtml?t=3549
    Sub writer_dumpFilterNames
       oFF = createUnoService( "com.sun.star.document.FilterFactory" )
       oFilterNames = oFF.getElementNames()
    
       ' Now print the filter names.
    '   For i = LBound( oFilterNames ) To UBound( oFilterNames )
    '      Print oFilterNames(i)
    '   Next
    
       ' Create a Writer doc and save the filter names to it.
       oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
       oText = oDoc.getText()
       oCursor = oText.createTextCursor()
       oCursor.gotoEnd( False )
    
       ' Print the filter names into a Writer document.
       For i = LBound( oFilterNames ) To UBound( oFilterNames )
          oText.insertString( oCursor, oFilterNames(i), False )
          oText.insertControlCharacter( oCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False )
       Next
    End Sub
    

    The code will create a new Writer document and put the names there (originally only printed the names, line 8-10)

    Does this help you?