为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕妹3146593 2022-09-06 21:23:55
我使用来自另一个数据帧的数据创建了一个数据透视表数据帧(df_pivot)。我创建了df_html = df_pivot.to_html()。然后我使用win32com(邮件。HTMLBody = df_html)发送一封正文中包含数据透视表的电子邮件。此功能工作正常。我希望能够在电子邮件中格式化表格(居中对齐,为单元格着色等),使其看起来更美观。
查看完整描述

1 回答

?
四季花海

TA贡献1811条经验 获得超5个赞

你可以看看熊猫。使您能够为数据帧提供样式的样式(即单元格颜色、对齐方式等)。


设置数据帧样式后,您可以使用他们的方法收集关联的 HTML,然后就开始了!render()


In [0]: df = pd.DataFrame([1])                                                                               


In [1]: html = df.style.set_properties(**{'background-color': 'black', 

    ...:                                  'color': 'lawngreen', 

    ...:                                  'border-color': 'white'}).render()

In [2]: print(html)                                             

Out[2]: <style type="text/css">

    #T_f804fbf6_6947_11ea_b4ca_8c8590b95ef2row0_col0 {

        background-color: black;

        color: lawngreen;

        border-color: white;

    }

</style>

<table id="T_f804fbf6_6947_11ea_b4ca_8c8590b95ef2">

    <thead>

        <tr>

            <th class="blank level0"></th>

            <th class="col_heading level0 col0">0</th>

        </tr>

    </thead>

    <tbody>

        <tr>

            <th id="T_f804fbf6_6947_11ea_b4ca_8c8590b95ef2level0_row0" class="row_heading level0 row0">0</th>

            <td id="T_f804fbf6_6947_11ea_b4ca_8c8590b95ef2row0_col0" class="data row0 col0">1</td>

        </tr>

    </tbody>

</table>'


查看完整回答
反对 回复 2022-09-06
  • 1 回答
  • 0 关注
  • 103 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号