1

Sorry, I am new to HTML and CSS.

The goal is to create HTML table from dataframe and send it via email.

But how can I stylish the output table? Let's I want different background header, font, etc?

This is the style I want to implement:

mystyle = 
'''
.mystyle {
    font-size: 11pt; 
    font-family: Arial;
    border-collapse: collapse; 
    border: 1px solid silver;

}

.mystyle td, th {
    padding: 5px;
}

.mystyle tr:nth-child(even) {
    background: #E0E0E0;
}

.mystyle tr:hover {
    background: silver;
    cursor: pointer;
}

'''

So how can I implement mystyle into below code and get stylish table? I tried df.to_html(classes=mystyle) but it did not work

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import pandas as pd
import datetime as dt

yesterday = dt.datetime.now() - dt.timedelta(days=1)
date = "'" + yesterday.strftime('%m-%d-%Y') + "'"


df = pd.DataFrame({
                'Position': ['DBA','CEO','Underwriter']
                ,'Salary': [100000,300000,60000]
                ,'Posted':['2019-01-01', '2019-05-01', '2019-03-15']
                ,'Link': ['myjob.com','ceo.com','insurance.com']
                })
html = """\
<html>
  <head>Report for """ + date + """</head>
  <body>
    {0}
  </body>
</html>
""".format(df.to_html())
print(html)

enter image description here

Serdia
  • 4,242
  • 22
  • 86
  • 159
  • Does this help? https://stackoverflow.com/questions/50807744/apply-css-class-to-pandas-dataframe-using-to-html/50939211 – Anis R. Jan 23 '20 at 16:33
  • Yes, I used that example. But the thing is they creating a .css file for 'mystyle'. So is any way to just create variable `mystyle` and then somehow call it into `df.to_html(classes=mystyle)` – Serdia Jan 23 '20 at 16:43

1 Answers1

4

You don't need to create a css file for this. Everything can work just fine if you concat them to a string. Here is how I normally do it:

message_start = f"""
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Report for {date}</title>"""
message_style = """
  <style type="text/css" media="screen">
    #customers {
      font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
      font-size: 12px;
      border-collapse: collapse;
      width: 100%;
    }

    #customers td, #customers th {
      border: 1px solid #ddd;
      padding: 8px;
    }

    #customers tr:nth-child(even){background-color: #f2f2f2;}

    #customers tr:hover {background-color: #ddd;}

    #customers th {
      padding-top: 12px;
      padding-bottom: 12px;
      text-align: left;
      background-color: #4CAF50;
      color: white;
    }
  </style>
</head>
<body>
"""
message_body = df.to_html(index=False, table_id="customers") #set table_id to your css style name
message_end = """</body>"""
messages = (message_start + message_style + message_body + message_end)
part = MIMEText(messages, 'html')  # create MIMEText
msg.attach(part)  
...
Henry Yik
  • 22,275
  • 4
  • 18
  • 40