-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcsvToHTML_bootstrap.py
executable file
·90 lines (71 loc) · 2.42 KB
/
csvToHTML_bootstrap.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
#!/usr/bin/env python3
import sys
import csv
from pathlib import Path
from textwrap import dedent
def main():
filepaths, delimiter = get_cli_args()
for f in filepaths:
abspath = Path(f).resolve()
htmlfilename = abspath.name.split(".")[0] + ".html"
htmlfile = abspath.parent.joinpath(htmlfilename)
# write the html file next to the csv with same name
with open(htmlfile, "wt", encoding="utf-8") as f:
print(f"Writing to {htmlfile.name}...")
f.write(get_html_header())
wroteHeader = False
for row in get_csv_rows(abspath, delimiter):
if not wroteHeader:
f.write('<thead><tr>')
for elem in row:
f.write(f"<td>{elem}</td>")
f.write("</tr></thead><tbody>")
wroteHeader = True
continue
f.write("<tr>")
for elem in row:
f.write(f"<td>{elem}</td>")
f.write("</tr>")
f.write("</tbody></table></div></body></html>")
print(f"Completed writing to {htmlfile.name}")
print("Done")
def get_csv_rows(csvpath, delimiter=","):
with open(csvpath, "r", encoding="latin-1") as f:
for row in csv.reader(f, delimiter=delimiter):
yield row
def get_html_header():
return dedent(
f"""
<html>
<head>
{get_bootstrap_cdn()}
</head>
<body>
<div class="container">
<table class="table table-striped">
"""
)
def get_bootstrap_cdn():
return dedent(
"""
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
"""
)
def get_cli_args():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"csv_file_paths", nargs=argparse.REMAINDER, help="One or more csv file paths"
)
parser.add_argument("-d", "--delimiter", help="Specify CSV delimiter")
args = parser.parse_args()
filepaths = args.csv_file_paths
delimiter = args.delimiter
if not delimiter:
delimiter = "," # default
return filepaths, delimiter
if __name__ == "__main__":
main()