small-hacks/ods2xls.py
Tom Marble 5906f15f4b ods2xls.py, sent to bkuhn by tmarble in this email:
Date: Thu, 03 Jan 2013 18:57:19 -0600
  From: Tom Marble <tmarble@info9.net>
  To: "Bradley M. Kuhn" <bkuhn@sfconservancy.org>
  Subject: ods2xlsx.py

Tom did not indicate the license of this file or ssconv.py, so I'm
waiting for him to clarify.

Tom mentioned this usage information in the email:

You will need:

These examples are adapted from:
http://www.linuxjournal.com/node/1007788
http://www.linuxjournal.com/node/1007797
  ^ I saved as ssconv.py and can share with you if you like

NOTE: you MUST save and exit LibreOffice first!
NOTE: you must save both *.py in the same directory.

I could not figure out the filter for XLSX as for XLS:
http://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options

Usage:

$ ./ods2xlsx.py ~/Documents/sample.ods ~/Documents/sample2.ods
/home/tmarble/Documents/sample.ods => /home/tmarble/Documents/sample.xls
Warning: -accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager is deprecated.  Use --accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager instead.
Warning: -norestore is deprecated.  Use --norestore instead.
Warning: -nofirststartwizard is deprecated.  Use --nofirststartwizard instead.
Warning: -nologo is deprecated.  Use --nologo instead.
Warning: -headless is deprecated.  Use --headless instead.
/home/tmarble/Documents/sample2.ods => /home/tmarble/Documents/sample2.xls
$
2014-09-04 11:41:06 -04:00

97 lines
3.6 KiB
Python
Executable file

#!/usr/bin/python
# ods2xlsx.py
# adapted from ssconv.py
# see also
# https://help.libreoffice.org/Common/About_Converting_Microsoft_Office_Documents
# http://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options
# http://linuxsleuthing.blogspot.com/2012/01/unoconv-is-number-one.html
import os
import ooutils
import uno
from com.sun.star.task import ErrorCodeIOException
class SSConverter:
"""
Spreadsheet converter class.
Converts spreadsheets to XLSX files.
"""
def __init__(self, oorunner=None):
self.desktop = None
self.oorunner = None
def convert(self, inputFile, outputFile):
"""
Convert the input file (a spreadsheet) to a XLSX file.
"""
# Start openoffice if needed.
if not self.desktop:
if not self.oorunner:
self.oorunner = ooutils.OORunner()
self.desktop = self.oorunner.connect()
inputUrl = uno.systemPathToFileUrl(os.path.abspath(inputFile))
outputUrl = uno.systemPathToFileUrl(os.path.abspath(outputFile))
document = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, ooutils.oo_properties(Hidden=True))
try:
# Additional property option:
# FilterOptions="59,34,0,1"
# 59 - Field separator (semicolon), this is the ascii value.
# 34 - Text delimiter (double quote), this is the ascii value.
# 0 - Character set (system).
# 1 - First line number to export.
#
# For more information see:
# http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options
#
# THE following works for xls
document.storeToURL(outputUrl, ooutils.oo_properties(FilterName="MS Excel 97"))
# THE following works for xlsx
#document.storeToURL(outputUrl, ooutils.oo_properties(FilterName="Microsoft Excel 2007/2010 XML"))
#document.storeToURL(outputUrl, ooutils.oo_properties(FilterName="MS Excel 2007/2010 XML"))
#document.storeToURL(outputUrl, ooutils.oo_properties(FilterName="MS Excel 2007/2010"))
#document.storeToURL(outputUrl, ooutils.oo_properties(FilterName="MS Excel 2007"))
#document.storeToURL(outputUrl, ooutils.oo_properties(FilterName="MS Excel 2010"))
#document.storeToURL(outputUrl, ooutils.oo_properties(FilterName="MS Excel 2010 XML"))
#document.storeToURL(outputUrl, ooutils.oo_properties(FilterName="MS Excel 2007 XML"))
finally:
document.close(True)
if __name__ == "__main__":
from sys import argv
from os.path import isfile
if len(argv) == 2 and argv[1] == '--shutdown':
ooutils.oo_shutdown_if_running()
else:
if len(argv) < 2:
print "USAGE:"
print " python %s INPUT-FILE [INPUT-FILE ...]" % argv[0]
print "OR"
print " python %s --shutdown" % argv[0]
exit(255)
if not isfile(argv[1]):
print "File not found: %s" % argv[1]
exit(1)
try:
i = 1
converter = SSConverter()
while i < len(argv):
odsname = argv[i]
#xlsxname = odsname.replace('.ods', '.xlsx')
xlsxname = odsname.replace('.ods', '.xls')
print '%s => %s' % (odsname, xlsxname)
converter.convert(odsname, xlsxname)
i += 1
except ErrorCodeIOException, exception:
print "ERROR! ErrorCodeIOException %d" % exception.ErrCode
exit(1)