-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlsx2json.py
64 lines (44 loc) · 1.51 KB
/
xlsx2json.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
# xlsx2json.py
# ---------
"""
xlsx2json.py reads excel sheets from '/ExcelSheets/' with query information and converts to json file
root_dir \
xlsx2json.py
ExcelSheets \
Events.xlsx
News.xlsx
json \
Events.xlsx
News.xlsx
"""
import xlrd
import os
from collections import OrderedDict
import simplejson as json
script_dir = (os.getcwd()) # getting file directory path
script_len = len(script_dir) # finding the length of directory path
sources = ['Events', 'News']
for i in range(len(sources)):
featureCollection = OrderedDict()
featureCollection['type'] = 'QueryCollection'
queries = []
wb = xlrd.open_workbook(script_dir + '/ExcelSheets/' + sources[i] + '.xlsx')
for j in range(len(wb.sheet_names())):
sh = wb.sheet_by_index(j)
query = OrderedDict()
query['type'] = 'Query'
parameters = []
for rownum in range(sh.nrows):
row_values = sh.row_values(rownum)
parameter = OrderedDict()
parameter['name'] = row_values[0]
parameter['value'] = row_values[1]
parameters.append(parameter)
query['Parameters'] = parameters
queries.append(query)
featureCollection['queries'] = queries
# Serialize the list of dicts to JSON
j = json.dumps(featureCollection)
# Write to file
with open(script_dir + '/json/' + sources[i] + '.json', 'w') as f:
f.write(j)