Python pandas to_html 使用 .style 选项还是自定义 CSS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36897366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas to_html using the .style options or custom CSS?
提问by trench
I was following the style guide for pandasand it worked pretty well.
我正在遵循熊猫的风格指南,它工作得很好。
How can I keep these styles using the to_html command through Outlook? The documentation seems a bit lacking for me.
如何通过 Outlook 使用 to_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'))
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
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.
to_html 文档显示有一个 classes 命令可以放在 to_html 方法中,但我无法弄清楚。我的数据框似乎也没有我在顶部指定的样式。
If I try:
如果我尝试:
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'))
Then df is now a Style object and you can't use to_html.
然后 df 现在是一个 Style 对象,您不能使用 to_html。
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.
编辑 - 这是我目前正在做的修改我的表格。这行得通,但我无法保留 Pandas 提供的 .style 方法的酷特性。
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>'''
回答by dennisobrien
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:
一旦你添加style
到你的链式分配中,你就是在对一个Styler
对象进行操作。该对象有一个render
方法可以将 html 作为字符串获取。因此,在您的示例中,您可以执行以下操作:
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()
)
Then include the html
in your email instead of a df.to_html()
.
然后html
在您的电子邮件中包含 ,而不是df.to_html()
。
回答by Plinio Bueno Andrade Silva
It's not an extravagant / pythonic solution. I inserted the link to a direct css file before the html code made by the to_html () method, then I saved the whole string as an html file. This worked well for me.
这不是一个奢侈的/pythonic 解决方案。我在to_html()方法制作的html代码之前插入了一个直接css文件的链接,然后我将整个字符串保存为一个html文件。这对我来说效果很好。
dphtml = r'<link rel="stylesheet" type="text/css" media="screen" href="css-table.css" />' + '\n'
dphtml += dp.to_html()
with open('datatable.html','w') as f:
f.write(dphtml)
f.close()
pass