Reputation: 69
I have a question. I need to add HTML boiler plate and additional data to a html file. I have a script that takes in a CSV file, processes it(clean, sort, etc) and use pandas .to_html. Problem is it gives me only table tags and the contnet inside. Content is something like this:
<table border="0" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>AK ID</th>
<th>TOODE</th>
<th>EAN</th>
<th>KOOD</th>
<th>ARVE</th>
<th>TK</th>
<th>XML</th>
</tr>
</thead>
<tbody>
<tr>
<td>369097</td>
<td>BENQ LED 21.5 GW2280 VA 0.248 FHD 1920x1080p 20M:1 (typ 3000:1) 250cd 178/178 5ms VGA/2xHDMI, TCO 7.0, Tilt, VESA, col: must</td>
<td>4718755073298</td>
<td>9H.LH4LB.QBE</td>
<td></td>
<td>1</td>
<td></td>
</tr>
<tr>
...
I have multiple CSV files and I need to automate it. What I need is to add html tags around my .to_html conent. Right now Im doing it manually and that takes time.
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="mycss.css">
</head>
<body>
<h1>Title</h1>
<h3>Secondary title</h3>
.to_html content
</body>
</html>
Upvotes: 0
Views: 204
Reputation: 2397
I'd recommend jinja2 for that, since compared to string concatenation, it's easier to debug later on. Here's how the code could look like:
EDIT: modified after comment
import os
import jinja2
# setup loader
templates_path = os.path.join(__file__, "../templates")
env = jinja2.Environment(loader=jinja2.FileSystemLoader(templates_path))
# get and fill the template
data_as_html = """<table border="0" class="dataframe">...</table>""" # from pandas
base_tmpl = env.get_template("base.html")
html = base_tmpl.render(data_as_html=data_as_html)
# write to disk
output_filename = os.path.join(__file__, "../out.html")
with open(output_filename, "w", encoding="utf-8") as f:
f.write(html)
With the template templates/base.html being something like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The output</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
{{ data_as_html }}
</body>
</html>
Upvotes: 1
Reputation: 20550
The output of .to_html()
is simply a string. So prepend and append whatever boilerplate you like when writing HTML files. Apparently you already have a function that can suitably parse an input CSV, so do something like this:
import glob
import re
for infile in glob.glob('*.csv'):
outfile = re.sub(r'\.csv$', '.html', infile)
df = parse_csv(infile)
with open(outfile, 'w') as fout:
fout.write(PREAMBLE + df.to_html() + POSTAMBLE)
Upvotes: 1