-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXLSX to json.py
66 lines (63 loc) · 2.81 KB
/
XLSX to json.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
import sys
import xlrd
def main():
for arg in sys.argv[1:]:
filename = arg
book = xlrd.open_workbook(filename)
f = open("XLSX_TO_JSON.json", "w")
sheetcount = book.nsheets-2
f.write("[" +"\n")
f.write(" {" +"\n")
f.write(" \""+ str(book.sheet_by_index(book.nsheets-2).cell(0,0).value) +"\""+":" +" \"" + str(book.sheet_by_index(book.nsheets-2).cell(0,1).value)+ "\"" + "," + "\n")
f.write(" \""+ str(book.sheet_by_index(book.nsheets-2).cell(1,0).value) +"\""+":" +" \"" + str(book.sheet_by_index(book.nsheets-2).cell(1,1).value)+ "\"" + "," + "\n")
f.write(" \""+ str(book.sheet_by_index(book.nsheets-2).cell(2,0).value) +"\""+":" +" \"" + str(book.sheet_by_index(book.nsheets-2).cell(2,1).value)+ "\"" + "," + "\n")
f.write(" \"data\": [" + "\n")
for I in range(sheetcount):
currsheet = book.sheet_by_index(I)
f.write(" {"+"\n")
f.write(" \"index\""+":" +" "+ str(I+1) + ","+"\n")
#string = string.replace("\n",' ')
f.write(" \"title\"" + ":" + " " + " \"" + str(currsheet.cell(0,1).value) + "\"" + ","+"\n")
string = " \"content\"" + ": " + " \""+str(currsheet.cell(1,1).value) + "\" " + ","
string = string.replace("\n",' ')
f.write(string)
f.write("\n")
labels = list
f.write(" \"labels\"" + ": " + "["+ "\"" +str(currsheet.cell(1,2).value) + "\""+ "]"+ ","+"\n")
f.write(" \"subheaders\"" + ": " + "["+"\n")
rows = currsheet.nrows
cols = currsheet.ncols
#print(rows)
#print(cols)
counter = 0
for i in range(1,rows-1,2):
counter = counter+1
f.write(" {"+"\n")
f.write(" \"index\""+":" +" "+ str(counter) + ","+"\n")
string1 = " \"content\":" + " \"" + str(currsheet.cell(i+2,1).value) + "\"" + ","
string1 = string1.replace("\n",' ')
f.write(string1)
f.write("\n")
if currsheet.cell(i+2,2).value == "":
f.write(" \"labels\"" + ": " + "[]")
else:
f.write(" \"labels\"" + ": " + "[" + "\"" + str(currsheet.cell(i+2,2).value) + "\""+ "]")
f.write("\n")
if i == rows-3:
f.write(" }")
else:
f.write(" },")
f.write("\n")
counter = 0
f.write(" ]"+"\n")
if I != book.nsheets-3:
f.write(" },")
if I == book.nsheets-3:
f.write(" }")
f.write("\n")
f.write(" ]"+"\n")
f.write(" }"+"\n")
f.write("]")
f.close()
if __name__ == "__main__":
main()