-
Notifications
You must be signed in to change notification settings - Fork 7
/
xls_xlsx_csv.py
98 lines (77 loc) · 3.31 KB
/
xls_xlsx_csv.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import sys
import os
import xlrd
import openpyxl
import unicodecsv
def xlsToCsvConvertor (xls_filename):
# Extract the filename along with path without extension.
csv_filename = xls_filename.rsplit('/', 1)[-1].rsplit('.', 1)[0]
try:
# It will load the workbook.
wb = xlrd.open_workbook(xls_filename)
# Check the number of sheets in the workbook.
sh = wb.nsheets
print("Number of sheets {}".format(sh))
shName = wb.sheet_names()
# Loop through the all the sheets.
for sheet_number in range(sh):
try:
# Open the sheet by index.
wsh = wb.sheet_by_index(sheet_number)
# Filename to generate the CSV file.
fileName = xls_filename.rsplit('/', 1)[0] + "/" + csv_filename + "-" + shName[sheet_number] + ".csv"
# Open the csv file in binary write mode.
fh = open(fileName, "wb")
# Uses unicodecsv, so it will handle Unicode characters.
csv_out = unicodecsv.writer(fh, encoding='utf-8')
# Loop through the rows of the sheet and write to csv file.
for row_number in range(wsh.nrows):
csv_out.writerow(wsh.row_values(row_number))
# Close the csv file.
fh.close()
print("CSV file created successfully.")
except:
print("Error creating CSV file.")
print(sys.exc_info())
except:
print("Error opening the file.")
print(sys.exc_info())
def xlsxToCsvHandler(xls_filename):
# Extract the filename along with path without extension.
csv_filename = xls_filename.rsplit('/', 1)[-1].rsplit('.', 1)[0]
# It will open the workbook.
try:
wb = openpyxl.load_workbook(xls_filename)
# Check the number of sheets in the workbook.
sh = len(wb.get_sheet_names())
print("Number of sheets {}".format(sh))
shName = wb.get_sheet_names()
# Loop through the all the sheets.
for sheet_number in range(sh):
try:
# Open the sheet by name.
wsh = wb[shName[sheet_number]]
# Filename to generate the CSV file.
fileName = xls_filename.rsplit('/', 1)[0] + "/" + csv_filename + "-" + shName[sheet_number] + ".csv"
# Open the csv file in binary write mode.
fh = open(fileName, "wb")
# Uses unicodecsv, so it will handle Unicode characters.
csv_out = unicodecsv.writer(fh, encoding='utf-8')
# Loop through the rows of the sheet and write to csv file.
for row in wsh.rows:
csv_out.writerow(cell.value for cell in row)
# Close the csv file.
fh.close()
print("CSV file created successfully.")
except:
print("Error creating CSV file.")
print(sys.exc_info())
except:
print("Error opening the file.")
print(sys.exc_info())
if __name__ == '__main__':
print(sys.argv[1].rsplit('/', 1)[-1].rsplit('.', 1)[-1])
if((sys.argv[1].rsplit('/', 1)[-1].rsplit('.', 1)[-1]) == "xls"):
xlsToCsvConvertor(sys.argv[1])
else:
xlsxToCsvHandler(sys.argv[1])