Skip to content

Instantly share code, notes, and snippets.

@danielecook
Created November 9, 2013 02:13
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 danielecook/7380695 to your computer and use it in GitHub Desktop.
Save danielecook/7380695 to your computer and use it in GitHub Desktop.
This quick function exports all of the workbooks within an excel file as individual csv's to make the data easier to work with.
import xlrd # pip install xlrd
import csv
import os
def export_workbook(filename):
# Open workbook for initial extraction
workbook = xlrd.open_workbook(filename)
filename = os.path.splitext(filename)[0] # Remove extension
if not os.path.exists(filename):
os.makedirs(filename)
# Iterate through each workbook.
for sheet in workbook.sheet_names():
worksheet = workbook.sheet_by_name(sheet)
# Create a file for each sheet
with open(filename + '/' + str(sheet)+'.csv','wb') as f:
c = csv.writer(f)
for r in range(worksheet.nrows):
c.writerow(worksheet.row_values(r))
print "Exported workbook '%s' %12.2d row%s" % (sheet,worksheet.nrows+85,"s"[worksheet.nrows==1:])
export_workbook('test.xlsx')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment