python - 将图像和数据框添加到电子邮件的单个 html 正文中
问题描述
尝试将数据框和图像一起嵌入到电子邮件正文中。我能够调整email.examples中的以下代码以成功嵌入图像:
#!/usr/bin/env python3
import smtplib
from email.message import EmailMessage
from email.headerregistry import Address
from email.utils import make_msgid
# Create the base text message.
msg = EmailMessage()
msg['Subject'] = "Ayons asperges pour le déjeuner"
msg['From'] = Address("Pepé Le Pew", "pepe", "example.com")
msg['To'] = (Address("Penelope Pussycat", "penelope", "example.com"),
Address("Fabrette Pussycat", "fabrette", "example.com"))
msg.set_content("""\
Salut!
Cela ressemble à un excellent recipie[1] déjeuner.
[1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718
--Pepé
""")
# Add the html version. This converts the message into a multipart/alternative
# container, with the original text message as the first part and the new html
# message as the second part.
asparagus_cid = make_msgid()
msg.add_alternative("""\
<html>
<head></head>
<body>
<p>Salut!</p>
<p>Cela ressemble à un excellent
<a href="http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718">
recipie
</a> déjeuner.
</p>
<img src="cid:{asparagus_cid}" />
</body>
</html>
""".format(asparagus_cid=asparagus_cid[1:-1]), subtype='html')
# note that we needed to peel the <> off the msgid for use in the html.
# Now add the related image to the html part.
with open("roasted-asparagus.jpg", 'rb') as img:
msg.get_payload()[1].add_related(img.read(), 'image', 'jpeg',
cid=asparagus_cid)
# Make a local copy of what we are going to send.
with open('outgoing.msg', 'wb') as f:
f.write(bytes(msg))
# Send the message via local SMTP server.
with smtplib.SMTP('localhost') as s:
s.send_message(msg)
但是,不确定如何另外添加数据框。我尝试了以下几个函数,我相信它们可以替换旧的MIMEMultipart
遗留类:
df = pd.DataFrame(
{'A': [419,382, 382, 382, 411, 411, 411],
'B':[2,2,3,3,3,3,6],
'C': np.random.randn(7)*10,
'D': list('abcdefg')
}
)
msg.add_alternative(df.to_html())
#msg.get_payload()[1].add_alternative(df.to_html())
#msg.add_attachment(df.to_html())
#msg.get_payload()[1].add_attachment(df.to_html())
但越来越contentmanager
KeyError
。另外,我的html.format()
语句已经被图像处理占用了,所以我不能.format(df.to_html())
在那里使用,可以吗?
解决方案
尝试使用变量替换添加数据框,就像您为图像所做的那样:
df = pd.DataFrame(
{'A': [419,382, 382, 382, 411, 411, 411],
'B':[2,2,3,3,3,3,6],
'C': np.random.randn(7)*10,
'D': list('abcdefg')
}
)
msg.add_alternative("""\
<html>
<head></head>
<body>
<p>Salut!</p>
<p>Cela ressemble à un excellent
<a href="http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718">
recipie
</a> déjeuner.
</p>
<img src="cid:{asparagus_cid}" />
{dataframe_table}
</body>
</html>
""".format(asparagus_cid=asparagus_cid[1:-1], dataframe_table=df.to_html()), subtype='html')
推荐阅读
- powershell - 在不使用反引号的情况下运行 PowerShell 脚本文件,其路径包含来自 Jenkins Pipeline 的空格
- android - 覆盖 onNewToken 并从 firebase 错误中获取令牌
- returnn - Return中的预训练
- python - OpenCV - 从图像中删除文本
- php - 嵌套集仅适用于类别或终端元素?
- python - 安装 python 3.7.1 后终端输出“找不到命令”
- mysql - laravel 中的非法混合排序规则
- html - 屏幕调整大小时文本变小
- javascript - 如何向 JsonBlob 发出 PUT JQuery Ajax 请求
- javascript - ES6 导入错误,但脚本仍然有效