Skip to content

Instantly share code, notes, and snippets.

@knight42
Created March 23, 2017 09:36
Show Gist options
  • Save knight42/757065ae249190e3ef5797185add3b49 to your computer and use it in GitHub Desktop.
Save knight42/757065ae249190e3ef5797185add3b49 to your computer and use it in GitHub Desktop.
Convert xls to csv
#!/usr/bin/python -O
# -*- coding: utf-8 -*-
########################
# Require python-xlrd
########################
from __future__ import print_function, unicode_literals, with_statement, division
import xlrd
import csv
from os import sys
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([worksheet_name,'.csv']), 'w')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in range(worksheet.nrows):
wr.writerow([entry for entry in worksheet.row_values(rownum)])
your_csv_file.close()
########################
# Usage:
# ./xls2csv.py your.xls
########################
if __name__ == "__main__":
csv_from_excel(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment