This repository was archived by the owner on Sep 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.py
More file actions
executable file
·84 lines (76 loc) · 2.17 KB
/
script.py
File metadata and controls
executable file
·84 lines (76 loc) · 2.17 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import sys
import xml.dom.minidom
import zipfile
def parse(raw_data): # return matrix
strs = raw_data.split("\n")
tbl = []
for s in strs:
if "<row" in s:
tbl.append([])
continue
if not "<c" in s:
continue
pos = s.find("s=\"")
value = int( s[pos + 3] )
tbl[-1].append(value)
return tbl
def to_html(tbl):
tmpl = "{}"
try:
tmpl_file = open("template.html", "r")
tmpl = tmpl_file.read()
tmpl_file.close()
except IOError:
print("Can't open 'template.html'")
htbl = "<table>\n"
for tr in tbl:
htbl += " <tr>\n"
for td in tr:
htbl += " <td class=\"s"+str(td)+"\"></td>\n"
htbl += " </tr>\n"
htbl += "</table>"
result = tmpl.replace("***table here***", htbl)
return result
def extract(arch_name, target):
result = ""
try:
arch = zipfile.ZipFile(arch_name, "r")
result = arch.read(target)
arch.close()
except (IOError, KeyError):
print("Incorrect archive provided")
return result
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: {} <xslx or xml file>".format(sys.argv[0]))
exit()
filename = sys.argv[1]
base = filename.split(".")[0]
ext = filename.split(".")[-1]
if len(filename.split(".")) < 2:
print("Incorrect filename specified")
exit()
raw_data = ""
if ext == "xlsx":
raw_data = extract(filename, "xl/worksheets/sheet1.xml")
elif ext == "xml":
try:
xml_file = open(filename, "r")
raw_data = xml_file.read()
xml_file.close()
except IOError:
print("Incorrect xml provided")
else:
print("Unsupported file provided")
xml = xml.dom.minidom.parseString(raw_data)
data = xml.toprettyxml()
table = parse(data)
html = to_html(table)
try:
output_file = open("{}.html".format(base), "w")
output_file.write(html)
output_file.close()
print("Result written to '{}.html'".format(base))
except IOError:
print("Can't write result")