Skip to content

Instantly share code, notes, and snippets.

@zfogg
Created December 16, 2014 03:59
Show Gist options
  • Save zfogg/ee194919a09660f7640d to your computer and use it in GitHub Desktop.
Save zfogg/ee194919a09660f7640d to your computer and use it in GitHub Desktop.
Convert a folder of CSVs into an Excel workbook, one worksheet per file.
#!/usr/bin/python
"""CSVsToWorksheets.py: takes a folder of CSVs and puts them into a single Excel workbook, one worksheet per file."""
__author__ = "Zach Fogg"
__email__ = "[email protected]"
import xlwt, csv, os
csv_folder = "Output/"
book = xlwt.Workbook(encoding="utf8")
for fil in os.listdir(csv_folder):
name = fil[:-18] # FIXME - clip the filenames to your liking
sheet = book.add_sheet(name)
with open(csv_folder + fil) as filname:
reader = csv.reader(filname)
i = 0
for row in reader:
for j, each in enumerate(row):
sheet.write(i, j, each)
i += 1
book.save("Output.xls")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment