1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #!/usr/bin/env python
# -*- coding: utf8 -*-
import xlrd
import unicodecsv as csv
from os import sys
def csv_from_excel(excel_file, encoding_override="utf-8"):
workbook = xlrd.open_workbook(excel_file)
#test de lecture
rs = workbook.sheet_by_index(0)
print rs.cell_value(1,0)
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']), 'wb')
class ExcelFr(csv.excel):
#Separateur de champ
delimiter = ";"
#
# Enregistre ce dialecte aupres du module csv
#
csv.register_dialect('excel-fr', ExcelFr())
wr = csv.writer(your_csv_file,'excel-fr', quoting=csv.QUOTE_ALL)
for rownum in xrange(worksheet.nrows):
wr.writerow([unicode(entry).encode("utf-8") for entry in worksheet.row_values(rownum)])
your_csv_file.close()
#if __name__ == "__main__":
# csv_from_excel(sys.argv[1])
csv_from_excel("source-2014-02-11.xls") |
Partager