python - HTML 表格无法在 Outlook 中打开 (pd.to_html)

标签 python html python-3.x email outlook

我正在使用 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/

相关文章:

python - 只从字符串 python 3 中删除一个字符一次

javascript - 通过CGI将图像从html发送到python脚本

html - 基于 CGI-BIN 的 Web 开发的主要缺点是什么?

python - 在 Mac 上使用 Python 3.5.0 + Sublime 3.0 运行代码

python - ValueError : not enough values to unpack (expected 2, 得到 1) NetworkX python 3

python - 如何从数据文件中读取指定间隔的行?

python - 在文件中逐行编辑 - Python

javascript - Bootmetro 框架,对齐问题

html - box-sizing + border + 绝对 child

python 源代码分发 (sdist) - 生成的数据文件