如何使用to_html将CSS类(我的样式)应用于Pandas DataFrame [英] How to apply CSS class (my style) to Pandas DataFrame using to_html

查看:83
本文介绍了如何使用to_html将CSS类(我的样式)应用于Pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我是HTML和CSS的新手.

Sorry, I am new to HTML and CSS.

目标是从数据框创建HTML表并通过电子邮件发送.

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?

这是我要实现的样式:

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;
}

'''

那么如何在以下代码中实现mystyle并获得时尚的表?我尝试了df.to_html(classes=mystyle),但没有成功

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)

推荐答案

您无需为此创建css文件.如果将它们连接为字符串,那么一切都可以正常工作.这是我通常的做法:

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)  
...

这篇关于如何使用to_html将CSS类(我的样式)应用于Pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆