如何在不丢失格式的情况下通过 e-mail 发送样式化的 pandas DataFrame?
How do I send a styled pandas DataFrame by e-mail without losing the format?
我对 Python 和 Jupyter 笔记本还很陌生。所以任何帮助将不胜感激。在 问题之后,我能够为 DataFrame 的特定单元格着色。
现在假设我有 table 上面有 "style"(作为接受的答案中的那个),我希望通过 e-mail 将它发送给某人(在在这种情况下,我自己)而不会丢失任何格式(它应该包含彩色单元格、边框和其他所有内容)。
我试过使用 .render() 函数,然后将 HTML 代码附加到 e-mail 的 body,但我一直得到无边框 table没有结构。我已经按照 post 对 e-mail 部分进行了编码。唯一的区别是我没有使用 "plain text" 部分。
此外,如何向 table 本身添加更多功能?比如添加table标题,修改列间距,增大或减小字号?
谢谢!
这是我在这种情况下使用的函数。 for 循环用于创建带状行。
def html_style_basic(df,index=True):
import pandas as pd
x = df.to_html(index = index)
x = x.replace('<table border="1" class="dataframe">','<table style="border-collapse: collapse; border-spacing: 0; width: 25%;">')
x = x.replace('<th>','<th style="text-align: left; padding: 2px; border-left: 1px solid #cdd0d4;" align="left">')
x = x.replace('<td>','<td style="text-align: left; padding: 2px; border-left: 1px solid #cdd0d4; border-right: 1px solid #cdd0d4;" align="left">')
x = x.replace('<tr style="text-align: right;">','<tr>')
x = x.split()
count = 2
index = 0
for i in x:
if '<tr>' in i:
count+=1
if count%2==0:
x[index] = x[index].replace('<tr>','<tr style="background-color: #f2f2f2;" bgcolor="#f2f2f2">')
index += 1
return ' '.join(x)
基于你已经通过获得了样式化的DataFrame,那么你可以将它保存到html文件中。
with open('DataFrame.html', 'a') as f:
f.write(df.render() )
如需进一步定制,请访问https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html。
我找到了问题的答案post:how to draw a beautiful colorful table with pandas or other package in Python?
基本上,如果您在为单元格添加颜色后使用.set_table_styles 添加边框,通过电子邮件发送的html 代码将正确显示。希望对遇到同样问题的人有所帮助。
我对 Python 和 Jupyter 笔记本还很陌生。所以任何帮助将不胜感激。在
现在假设我有 table 上面有 "style"(作为接受的答案中的那个),我希望通过 e-mail 将它发送给某人(在在这种情况下,我自己)而不会丢失任何格式(它应该包含彩色单元格、边框和其他所有内容)。
我试过使用 .render() 函数,然后将 HTML 代码附加到 e-mail 的 body,但我一直得到无边框 table没有结构。我已经按照 post 对 e-mail 部分进行了编码。唯一的区别是我没有使用 "plain text" 部分。
此外,如何向 table 本身添加更多功能?比如添加table标题,修改列间距,增大或减小字号?
谢谢!
这是我在这种情况下使用的函数。 for 循环用于创建带状行。
def html_style_basic(df,index=True):
import pandas as pd
x = df.to_html(index = index)
x = x.replace('<table border="1" class="dataframe">','<table style="border-collapse: collapse; border-spacing: 0; width: 25%;">')
x = x.replace('<th>','<th style="text-align: left; padding: 2px; border-left: 1px solid #cdd0d4;" align="left">')
x = x.replace('<td>','<td style="text-align: left; padding: 2px; border-left: 1px solid #cdd0d4; border-right: 1px solid #cdd0d4;" align="left">')
x = x.replace('<tr style="text-align: right;">','<tr>')
x = x.split()
count = 2
index = 0
for i in x:
if '<tr>' in i:
count+=1
if count%2==0:
x[index] = x[index].replace('<tr>','<tr style="background-color: #f2f2f2;" bgcolor="#f2f2f2">')
index += 1
return ' '.join(x)
基于你已经通过
with open('DataFrame.html', 'a') as f:
f.write(df.render() )
如需进一步定制,请访问https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html。
我找到了问题的答案post:how to draw a beautiful colorful table with pandas or other package in Python?
基本上,如果您在为单元格添加颜色后使用.set_table_styles 添加边框,通过电子邮件发送的html 代码将正确显示。希望对遇到同样问题的人有所帮助。