-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
70 lines (62 loc) · 2.2 KB
/
index.py
File metadata and controls
70 lines (62 loc) · 2.2 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# export data sheets from xlsx to csv
from openpyxl import load_workbook
import csv
import os
from os import sys
def get_all_files(in_dir):
file_pathes = []
for root, dirs, files in os.walk(in_dir):
for file_name in files:
file_pathes.append(''.join([in_dir, '/', file_name]))
return file_pathes
def get_all_sheets(excel_file):
sheets = []
workbook = load_workbook(excel_file)
all_worksheets = workbook.get_sheet_names()
for worksheet_name in all_worksheets:
sheets.append(worksheet_name)
return sheets
def csv_from_excel(excel_file, sheets, out_dir):
excel_file_name = excel_file.rsplit('/', 1)[1].split('.')[0];
print("File " + excel_file_name + " ...")
print("")
workbook = load_workbook(excel_file,data_only=True)
for worksheet_name in sheets:
print(" Sheet " + worksheet_name + " ...")
try:
worksheet = workbook.get_sheet_by_name(worksheet_name)
except KeyError:
print("Could not find " + worksheet_name)
sys.exit(1)
dir_path = ''.join([out_dir.decode('utf_8'), '/', excel_file_name.decode('utf_8')]);
if not os.path.exists(dir_path):
os.makedirs(dir_path);
file_path = ''.join([dir_path, '/', worksheet_name,'.csv']);
print(" Save as " + file_path + " ...")
your_csv_file = open(file_path, 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for row in worksheet.iter_rows():
lrow = []
for cell in row:
if isinstance(cell.value, unicode):
lrow.append(cell.value.encode('utf_8'))
else:
lrow.append(cell.value)
wr.writerow(lrow)
print(" ... done")
print("")
workbook.close()
if len(sys.argv) != 3:
print("Call with " + sys.argv[0] + " <in dir> <out dir>")
sys.exit(1)
else:
in_dir = sys.argv[1]
out_dir = sys.argv[2]
files = get_all_files(in_dir)
print files
for file_path in files:
sheets = get_all_sheets(file_path)
assert(sheets != None and len(sheets) > 0)
csv_from_excel(file_path, sheets, out_dir)