-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable2html.py
More file actions
84 lines (69 loc) · 3.5 KB
/
table2html.py
File metadata and controls
84 lines (69 loc) · 3.5 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
#########################################################################
"""
Script built upon the code provide by Rosetta Code at:
http://rosettacode.org/wiki/CSV_to_HTML_translation
Content is available under GNU Free Documentation License 1.2.
BeautifulHTML Package Index Owner: alice1017
## MODIFIED BY: Melinda Ashcroft 10/09/2014
## MELINDA AFFILIATIONS: Beatson lab | SCMB - UQ
"""
#########################################################################
# Import modules required
import sys
from cgi import escape
from Bhtml import BeautifulHTML
# Open files as variables using arguments passed to the sys module via the command line
with open(sys.argv[1], 'r') as fin: # Open the infile (typically tab delimited file)
with open(sys.argv[2], 'w') as fout: # Open the outfile (html)
# Read in the file line by line and store in a variable
rawdata = fin.read()
# Create the html headers, css elements and body tags
html = BeautifulHTML()
html1 = BeautifulHTML()
html.write("<html>")
html.indent("<head>")
css = html.setcss()
css.element("body",{"background":"#fff","color":"#000"})
css.element("table",{"width":"100%","background":"#fff","color":"#000", "border":"1px solid #666"})
css.element("tbody:last-child",{"display":"none"})
css.element("td",{"border-right":"1px solid #666"})
css.element("td:last-child",{"border":"0"})
html.writecss(css.release(), indent=True, block=2)
html.indent("</head>")
html.indent("<body>")
html1.indent("</body>")
# Function 1 to read in each row of input file and split on the tab delimiter generating rows and columns
def _row2trextra(row, attr=None):
cols = escape(row).split('\t')
attr_tr = attr.get('TR', '')
attr_td = attr.get('TD', '')
return (('<TR%s>' % attr_tr)
+ ''.join('<TD%s>%s</TD>' % (attr_td, data) for data in cols)
+ '</TR>')
# Function 2 to build the html table based upon the input tab delimited file
def csv2htmlextra(txt, header=True, attr=None):
' attr is a dictionary mapping tags to attributes to add to that tag'
attr_table = attr.get('TABLE', '')
attr_thead = attr.get('THEAD', '')
attr_tbody = attr.get('TBODY', '')
htmltxt = '<TABLE%s>\n' % attr_table
for rownum, row in enumerate(txt.split('\n')):
htmlrow = _row2trextra(row, attr)
rowclass = ('THEAD%s' % attr_thead) if (header and rownum == 0) else ('TBODY%s' % attr_tbody)
htmlrow = '<%s>%s</%s>\n' % (rowclass, htmlrow, rowclass[:0])
htmltxt += htmlrow
htmltxt += '</TABLE>\n'
return htmltxt
# Generate the html variable by building a html table from the dictionary of Function 2, using basic html tags
# Change summary="Table Heading" to whatever title you'd like for the table
htmltxt = csv2htmlextra(rawdata, True,
dict(TABLE=' cellspacing="0", cellpadding="5", summary="Table Heading"',
THEAD=' style="text-align: center; font-weight: bold; border: 1px solid #000; background-color: lightgrey;"',
TBODY=''
)
)
# Write the html variables to the output html file
fout.write(str(html))
fout.write(htmltxt)
fout.write(str(html1))
fout.write("</html>")