Skip to content

Instantly share code, notes, and snippets.

@monkeycycle
Forked from gka/xls2csv.py
Created May 8, 2017 13:56
Show Gist options
  • Save monkeycycle/90330ad878092577f51318cc807cad22 to your computer and use it in GitHub Desktop.
Save monkeycycle/90330ad878092577f51318cc807cad22 to your computer and use it in GitHub Desktop.
Excel to CSV
import xlrd
import csv
import sys
def csv_from_excel(fn):
wb = xlrd.open_workbook(fn)
sh = wb.sheet_by_index(0)
your_csv_file = open(fn.replace('xls', 'csv'), 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in xrange(sh.nrows):
row = sh.row_values(rownum)
print row
for i in range(len(row)):
if isinstance(row[i], basestring):
row[i] = row[i].encode('utf-8')
wr.writerow(row)
your_csv_file.close()
for f in sys.argv[1:]:
csv_from_excel(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment