pandas to_html使用.style选项还是自定义CSS? [英] pandas to_html using the .style options or custom CSS?

查看:151
本文介绍了 pandas to_html使用.style选项还是自定义CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在跟踪熊猫样式指南,并且效果很好出色地.

I was following the style guide for pandas and it worked pretty well.

如何通过Outlook的to_html命令保留这些样式?文件似乎对我来说有点缺乏.

How can I keep these styles using the to_html command through Outlook? The documentation seems a bit lacking for me.

(df.style
   .format(percent)
   .applymap(color_negative_red, subset=['col1', 'col2'])
   .set_properties(**{'font-size': '9pt', 'font-family': 'Calibri'})
   .bar(subset=['col4', 'col5'], color='lightblue'))

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.Subject = subject_name
mail.HTMLbody = ('<html><body><p><body style="font-size:11pt; 
font-family:Calibri">Hello,</p> + '<p>Title of Data</p>' + df.to_html(
            index=False, classes=????????) '</body></html>')
mail.send

to_html文档显示,我可以在to_html方法中放入一个类命令,但无法弄清楚.似乎我的数据框也没有采用我自上而下指定的样式.

The to_html documentation shows that there is a classes command that I can put inside of the to_html method, but I can't figure it out. It also seems like my dataframe does not carry the style that I specified up top.

如果我尝试:

 df = (df.style
       .format(percent)
       .applymap(color_negative_red, subset=['col1', 'col2'])
       .set_properties(**{'font-size': '9pt', 'font-family': 'Calibri'})
       .bar(subset=['col4', 'col5'], color='lightblue'))

然后df现在是一个Style对象,您不能使用to_html.

Then df is now a Style object and you can't use to_html.

编辑-这是我目前正在做的修改表的工作.这行得通,但是我不能保留pandas提供的.style方法的出色功能.

Edit - this is what I am currently doing to modify my tables. This works, but I can't keep the cool features of the .style method that pandas offers.

email_paragraph = """
<body style= "font-size:11pt; font-family:Calibri; text-align:left; margin: 0px auto" >
"""

email_caption = """
<body style= "font-size:10pt; font-family:Century Gothic; text-align:center; margin: 0px auto" >
"""


email_style = '''<style type="text/css" media="screen" style="width:100%">
    table, th, td {border: 0px solid black;  background-color: #eee; padding: 10px;}
    th {background-color: #C6E2FF; color:black; font-family: Tahoma;font-size : 13; text-align: center;}
    td {background-color: #fff; padding: 10px; font-family: Calibri; font-size : 12; text-align: center;}
  </style>'''

推荐答案

style添加到链式分配后,您将在Styler对象上进行操作.该对象具有render方法来获取html作为字符串.因此,在您的示例中,您可以执行以下操作:

Once you add style to your chained assignments you are operating on a Styler object. That object has a render method to get the html as a string. So in your example, you could do something like this:

html = (
    df.style
    .format(percent)
    .applymap(color_negative_red, subset=['col1', 'col2'])
    .set_properties(**{'font-size': '9pt', 'font-family': 'Calibri'})
    .bar(subset=['col4', 'col5'], color='lightblue')
    .render()
)

然后在电子邮件中包含html而不是df.to_html().

Then include the html in your email instead of a df.to_html().

这篇关于 pandas to_html使用.style选项还是自定义CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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