I am trying to translate a csv into an html table, insert it into an email and send it out. I found a similar question Here and seems to work great.
My email is sending out but no matter what I do I cannot seem to style the table. Ideally I want to add a colored title to the table and possible modify the borders. This is going to be something that is automatically sent every week so I want it as automated as possible.
with open('fileTest.html', 'r') as f:
x = f.read()
f.close()
html = x
with open('testy.csv') as input_file:
reader = csv.reader(input_file)
data = list(reader)
#text = text.format(table=tabulate(data, headers="firstrow", tablefmt="html"))
html = html.format(table=tabulate(data, headers="firstrow", tablefmt="html"))
message = MIMEMultipart(
"alternative", None, [MIMEText(text), MIMEText(html, 'html')])
This is and example of the code I have, the full version is basically the same as the answer in the link I shared.
If I try to add a style tag to the top of the file then the .format()
errors out with a key error.
EDIT
I should mention that I am generating my html from pandas to_html
import pandas as pd
from IPython.display import HTML
df = pd.read_csv('testy.csv')
df.to_html('fileTest.html')
I have also tried it this way:
HTML(df.to_html(filetest.html))
Here is an example of the first couple rows of output
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Name of Region</th>
<th>Number Sent To</th>
<th>YTD Calls</th>
<th>MTD Calls</th>
<th>Week of</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Windham</td>
<td>18032515985</td>
<td>3</td>
<td>1</td>
<td>0</td>
</tr>
Here is a link to the docs on styling them. But I find them lacking