有没有办法格式化数据透视表并将其嵌入到python的电子邮件中?

我使用来自另一个数据帧的数据创建了一个数据透视表数据帧(df_pivot)。我创建了df_html = df_pivot.to_html()。

然后我使用win32com(邮件。HTMLBody = df_html)发送一封正文中包含数据透视表的电子邮件。此功能工作正常。

我希望能够在电子邮件中格式化表格(居中对齐,为单元格着色等),使其看起来更美观。


慕妹3146593
浏览 104回答 1
1回答

四季花海

你可以看看熊猫。使您能够为数据帧提供样式的样式(即单元格颜色、对齐方式等)。设置数据帧样式后,您可以使用他们的方法收集关联的 HTML,然后就开始了!render()In [0]: df = pd.DataFrame([1])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;In [1]: html = df.style.set_properties(**{'background-color': 'black',&nbsp;&nbsp; &nbsp; ...:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'color': 'lawngreen',&nbsp;&nbsp; &nbsp; ...:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'border-color': 'white'}).render()In [2]: print(html)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[2]: <style type="text/css">&nbsp; &nbsp; #T_f804fbf6_6947_11ea_b4ca_8c8590b95ef2row0_col0 {&nbsp; &nbsp; &nbsp; &nbsp; background-color: black;&nbsp; &nbsp; &nbsp; &nbsp; color: lawngreen;&nbsp; &nbsp; &nbsp; &nbsp; border-color: white;&nbsp; &nbsp; }</style><table id="T_f804fbf6_6947_11ea_b4ca_8c8590b95ef2">&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th class="blank level0"></th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th class="col_heading level0 col0">0</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th id="T_f804fbf6_6947_11ea_b4ca_8c8590b95ef2level0_row0" class="row_heading level0 row0">0</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td id="T_f804fbf6_6947_11ea_b4ca_8c8590b95ef2row0_col0" class="data row0 col0">1</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </tbody></table>'
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python