0

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

2 Answers 2

1

Instead using to_html styling, use style.

import pandas as pd

def style_line(s):
    '''Rendering odd and even rows with different color'''
    return ['background-color: #D4E6F1' if i%2!=0 else 'background-color: #85C1E9' for i in range(len(s))]


df = pd.read_csv('testy.csv')
# df.to_html('fileTest.html')
df = pd.DataFrame(df)
df = df.style.apply(style_line).render()
print(df)
Sign up to request clarification or add additional context in comments.

Comments

0

After the html = html.format(table=tabulate(data, headers="firstrow", tablefmt="html")) line, why not do something like:

style = '''
<style>
    #headings {
    font-size:26px !important;
    line-height:32px !important;
    }
</style>
'''

html = style + str(html)
message = MIMEMultipart(
    "alternative", None, [MIMEText(text), MIMEText(html, 'html')])

This should style your table.

2 Comments

Tried it out and it had no effect on the table
Yea, I will edit it in. I also did more research and I am going to modify my question because I left something out that can help. I am generating my html with pandas to_html method. I think there is a way to style this but can't figure it out

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.