Skip to content

Instantly share code, notes, and snippets.

@jehiah jehiah/
Created May 18, 2017

What would you like to do?
Convert xls to CSV
This package uses openpyxl and xlrd for the widest coverage in converting XLS to CSV
import openpyxl
from openpyxl.utils.exceptions import InvalidFileException
import tornado.options
import os.path
import csv
import sys
import xlrd
def _utf8(s):
"""encode a unicode string as utf-8"""
if isinstance(s, unicode):
return s.encode("utf-8")
assert isinstance(s, str), "_utf8 expected a str, not %r" % type(s)
return s
def to_string(v):
if v is None:
return ''
if isinstance(v, int):
return str(v)
return _utf8(v)
if __name__ == "__main__":
tornado.options.define("input_file", default=None, type=str)
tornado.options.define("worksheet", default=0, type=int)
assert os.path.exists(tornado.options.options.input_file)
w = csv.writer(sys.stdout)
wb = openpyxl.load_workbook(filename=tornado.options.options.input_file, read_only=True)
ws = wb.worksheets[tornado.options.options.worksheet]
for row in ws.rows:
w.writerow([to_string(cell.value) for cell in row])
except InvalidFileException:
# retry with xlrd
wb = xlrd.open_workbook(filename=tornado.options.options.input_file)
ws = wb.sheet_by_index(tornado.options.options.worksheet)
for row in ws.get_rows():
w.writerow([to_string(cell.value) for cell in row])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.