Skip to content

Instantly share code, notes, and snippets.

@elaOnMars
Forked from dannguyen/xlrd-xls-to-csv--treer.py
Created April 12, 2016 15:47
Show Gist options
  • Save elaOnMars/f84e84acd796b8a3e36988dd94cdb278 to your computer and use it in GitHub Desktop.
Save elaOnMars/f84e84acd796b8a3e36988dd94cdb278 to your computer and use it in GitHub Desktop.
example of using Python's xlrd library to do a batch process of opening a Excel workbook, extract the content from the first sheet as txt, and then save as CSV
"""
This code processes each xlsx file in the xlsx/* path,
then writes the data (a list of values) to CSV files stored in the csv/ path
"""
from csv import writer
from glob import glob
from os.path import basename, join
from os import makedirs
from xlrd import open_workbook
import re
CSV_DIRNAME = 'csv'
makedirs(CSV_DIRNAME, exist_ok=True)
for xfname in glob(join('xlsx', '*', '*.xlsx')):
cname = join(CSV_DIRNAME, basename(xfname) + '.csv')
print("writing to", cname)
cf = open(cname, 'w')
cv = writer(cf)
book = open_workbook(xfname)
sheet = book.sheets()[0]
for n in range(sheet.nrows):
rowvals = sheet.row_values(n)
cv.writerow(rowvals)
cf.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment