自由办公室uno api保存为xls

时间:2015-04-08 06:18:31

标签: python export-to-excel uno

到目前为止,我有以下代码强制ods重新计算spred sheet中的所有值并将其保存为多种格式。

但是我找不到XLS的过滤器。 有谁知道如何找到它?

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()

1 个答案:

答案 0 :(得分:2)

使用以下基本宏获取所有可用过滤器名称的列表

' 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

代码将创建一个新的Writer文档并将名称放在那里(最初只打印名称,第8-10行)

这对你有帮助吗?