Skip to content

Instantly share code, notes, and snippets.

@gka
Created September 17, 2015 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gka/fa96bb02a102bc5be6cc to your computer and use it in GitHub Desktop.
Save gka/fa96bb02a102bc5be6cc 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