【问题标题】:How to format pandas df.to_html table with conditional formatting to Outlook?如何将带有条件格式的 pandas df.to_html 表格式化为 Outlook?
【发布时间】:2019-09-13 15:30:51
【问题描述】:

我有一个代码,我从数据框中计算几个值并生成要传递给 Outlook 的 html。这个 html 也有一个我想要格式化的数据框表。但是 df.to_html 会生成一个未格式化的表格,我想根据条件应用条件格式。

上面的整个输出我想传递给 Outlook 以作为消息发送。

import win32com


o = win32com.client.Dispatch("Outlook.Application")
Msg = o.CreateItem(0)
Msg.HTMLBody = '<html><body>Today is {aaj}.<br><b>Pandas Table Output :</b> <br>{ComputerList}<br></body></html>'.format(aaj=din , ComputerList=SystemList[['System Name','IP Address','Updated','Value','OS Type','Status']].to_html(header=True, index=False, border=None))
Msg.Display()

运行代码时,输​​出如下图

我想为一列应用一些格式,如边框和单元格背景颜色,或在一列中应用条形符号。 在阅读了多页之后,我仍然无法做到这一点。 非常感谢任何工作帮助。

【问题讨论】:

    标签: python html python-3.x pandas outlook


    【解决方案1】:

    您可以使用“pretty-html-table”包来为您完成这项工作。

    代码看起来像这样,您可以在其中提供您选择的数据框:

    from send_email import send_mail
    from get_data import get_gdp_data
    from pretty_html_table import build_table
    
    
    def send_country_list():
        gdp_data = get_gdp_data()
        output = build_table(gdp_data, 'blue_light')
        send_mail(output)
        return "Mail sent successfully."
    
    
    send_country_list()
    

    更详细的解释可以查看:https://dev.to/siddheshshankar/convert-a-dataframe-into-a-pretty-html-table-and-send-it-over-email-4663

    【讨论】:

    • 我会再次执行这个来验证它是否有效。
    猜你喜欢
    • 2016-01-18
    • 2020-04-08
    • 2013-01-15
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 2019-06-02
    相关资源
    最近更新 更多