-
-
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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