Skip to content

Instantly share code, notes, and snippets.

@akanik
Last active May 15, 2018 15:26
Show Gist options
  • Save akanik/d2ffc4cb03ec09bda43b to your computer and use it in GitHub Desktop.
Save akanik/d2ffc4cb03ec09bda43b to your computer and use it in GitHub Desktop.
excel sheets to individual csv files
# Thanks to http://stackoverflow.com/questions/9884353/xls-to-csv-convertor
import os, csv, xlrd
excel_file = '/path/to/file.xls'
#we're specifying a directory here because we'll have several files
#be sure to include end slash
csv_filepath = 'path/to/csv/directory/'
def csv_from_excel(excel_file):
workbook = xlrd.open_workbook(excel_file)
all_worksheets = workbook.sheet_names()
for worksheet_name in all_worksheets:
worksheet = workbook.sheet_by_name(worksheet_name)
your_csv_file = open(''.join([csv_filepath,worksheet_name,'.csv']), 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in xrange(worksheet.nrows):
wr.writerow([unicode(entry).encode("utf-8") for entry in worksheet.row_values(rownum)])
your_csv_file.close()
csv_from_excel(excel_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment