我正在使用 pd.to_html 通过电子邮件发送我的 pandas 数据框,电子邮件在 Gmail 浏览器中可以正常打开,并且电子邮件正文中包含所有表格。但他们并没有在 Outlook 中打开,所有表格都以 HTML 附件的形式在 Outlook 中打开。
使用以下代码将我的数据框转换为 HTML。
df_1 = df_1.to_html(index=False,header=True,border=4,justify = 'left',col_space =9)
从 IPython.core.display 导入显示,HTML 显示(HTML(df_1))
这就是我发送电子邮件的方式:-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
email_user = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96eeefecd6eeb8f5f9fb" rel="noreferrer noopener nofollow">[email protected]</a>'
email_password = '*********'
email_send = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f2726251f27713c3032" rel="noreferrer noopener nofollow">[email protected]</a>'
subject = 'ABCDEF'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body_1 = """ Hey All,
Please find the data:
"""
msg.attach(MIMEText(body_1,'plain'))
msg.attach(MIMEText(df_1,'html'))
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,email_password)
server.sendmail(email_user,email_send.split(','),text)
server.quit()
没有错误,电子邮件可以在 Gmail 中正常打开。但 Outlook 中的表格是作为附件提供的。
最佳答案
您可以将单个附件制作为 html。这对我有用。
df_1 = df_1.to_html(index=False,header= True,border=4,justify = 'left',col_space =9)
mailBody= """<p> Hi All, <br><br>
Please find the data: </p> <br><br>
""" + df_1 + """<br> Thanks. """
msg.attach(MIMEText(mailBody,'html'))
关于python - HTML 表格无法在 Outlook 中打开 (pd.to_html),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56318372/