Skip to content

Instantly share code, notes, and snippets.

@pyykkis
Created September 24, 2013 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyykkis/6689623 to your computer and use it in GitHub Desktop.
Save pyykkis/6689623 to your computer and use it in GitHub Desktop.
Convert xls and xlsx files to csv and print to stdout.
#!/usr/bin/python
from sys import *
import xlrd
import csv
def to_utf8(val):
if isinstance(val, basestring):
return val.encode('utf-8')
else:
return val
workbook = xlrd.open_workbook(argv[1])
sheet = workbook.sheet_by_index(0)
csv_writer = csv.writer(stdout)
for i in xrange(sheet.nrows):
csv_writer.writerow(map(to_utf8, sheet.row_values(i)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment