pythonlibreofficeheadlessunopyuno

unotools - try to convert ods or excel files to csv using python


What I need is a command line tool to convert excel and ods spreadsheet files to csv which I can use on a web server (Ubuntu 16.04). I already red this: https://pypi.python.org/pypi/unotools which works fine for the given examples.

And this: http://www.linuxjournal.com/content/convert-spreadsheets-csv-files-python-and-pyuno-part-1v2 which should do the work I want it to do, but does not in my environment.

My problem I think is in the method Calc.store_to_url:

Line throwing exception

component.store_to_url(url,'FilterName','Text - txt - csv (StarCalc)')

I really would appreciate a hint.

Exception

unotools.unohelper.ErrorCodeIOException: SfxBaseModel::impl_store failed: 0x81a

Full source

import sys
from os.path import basename, join as pathjoin, splitext

from unotools import Socket, connect
from unotools.component.calc import Calc
from unotools.unohelper import convert_path_to_url
from unotools import parse_argument



def get_component(args, context):
    _, ext = splitext(args.file_)
    url = convert_path_to_url(args.file_)
    component = Calc(context, url)
    return component

def convert_csv(args, context):
    component = get_component(args, context)
    url = 'out/result.csv'    
    component.store_to_url(url,'FilterName','Text - txt - csv (StarCalc)')
    component.close(True)


args = parse_argument(sys.argv[1:])
context = connect(Socket(args.host, args.port), option=args.option)

convert_csv(args, context)

Solution

  • The URL must be in file:// format.

    url = convert_path_to_url('out/result.csv')
    

    See the store_to_url example at https://pypi.python.org/pypi/unotools.

    EDIT:

    To use the absolute path, choose one of these; there is no need to combine them.

    url = 'file:///home/me/out/result.csv'
    url = convert_path_to_url('/home/me/out/result.csv')
    

    To use the relative path, first verify that the working directory is '/home/me' by calling os.getcwd().