开发者社区> 问答> 正文

有没有一种方法可以格式化数据透视表并将其嵌入python中的电子邮件中?

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

然后,我使用win32com(mail.HTMLBody = df_html)发送带有主体中枢轴表的电子邮件。此功能运行良好。

我希望能够对电子邮件中的表格进行格式化(居中对齐,对单元格进行着色等),使其看起来更像样。

问题来源:stackoverflow

展开
收起
is大龙 2020-03-23 12:35:08 522 0
1 条回答
写回答
取消 提交回答
  • 您可以看一下pandas.Style,它将使您能够给数据框指定样式(即单元格颜色,对齐方式等)。

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

    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>'
    

    回答来源:stackoverflow

    2020-03-23 12:35:12
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载