首页 > 解决方案 > Python 3: pretty_html_table returning HTML text rather than table

问题描述

I'm writing a script to email a pandas dataframe as a table to several people. I have my dataframe frame:

     SITE  ...   VISITS
438     1  ...      104
439     2  ...      104
440     3  ...      104
504     4  ...      215

I'm trying to create a pretty_html_table that I can email using:

from pretty_html_table import build_table

html_table = build_table(frame, 'blue_light')

However, instead of emailing a table, the script emails a bunch of html text!

<p><table border="0" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th style = "background-color: #FFFFFF;font-family: Century Gothic;font-size: medium;color: #305496;text-align: left;border-bottom: 2px solid #305496;padding: 0px 20px 0px 0px">SITE</th>
etcetera, etcetera ...     

I'm trying to append this table to an email like so:

import win32com.client as win32

def report_email(email):        
    html_table = build_table(frame, 'blue_light')
    
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = email
    mail.Subject = 'Table below'
    mail.Body  = "Hi, here is a table" + html_table
    
    return mail.Send()

Thanks so much for all you help!

标签: pythonhtml

解决方案


pretty_html_table.build_table预计会返回表格的 HTML 代码。问题是您使用mail.Body. 改为使用mail.HTMLBody

这是MailItem.HTMLBody 文档


推荐阅读