python - 使用 python 3 在电子邮件中嵌入 Pandas df html 表
问题描述
我正在尝试嵌入和发送用pandas
.to_html
.
我很高兴将 df 直接发送到电子邮件或从文件中。
到目前为止,我可以使用以下内容嵌入图像:
fp = open(attachment, 'rb')
img = MIMEImage(fp.read())
fp.close()
img.add_header('Content-ID', '<{}>'.format(attachment))
msg.attach(img)
我已经对此进行了试验,它附加了 df 但由于某种原因没有嵌入。
fp = open(att1, 'r')
img = MIMEText(fp.read(),'html')
fp.close()
img.add_header('Content-ID', '<att1>')
msg.attach(img)
或者,我可以将 df 数据发送到电子邮件,但作为未格式化的文本发送,并且似乎无法使用带有简单边框的格式化(即)表格来解决这种方法。
df = dFrame(q2)
tbl = '{df}'
tbl = df.to_html(index=False,justify='center')
msgText = MIMEText('<b>%s</b><br><html src="cid:%s"><br>' % (body, tbl), 'html')
msg.attach(msgText)
我希望为嵌入 html 表调整的嵌入图像的更完整代码。
def sndFile1():
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
att1 = path + 'df.html'
att2 = path + 'Indices.png'
att3 = path + 'Segments.png'
subject = 'Market Update'
body = 'This Weeks Report'
msg = MIMEMultipart()
msg["To"] = myEml
msg["From"] = myEml
msg["Subject"] = subject
msgText = MIMEText('<b>%s</b><br><html src="cid:%s"><img src="cid:%s"><img src="cid:%s"><br>' % (body, att1, att2, att3), 'html')
msg.attach(msgText)
fp = open(att1, 'r')
img = MIMEText(fp.read(),'html')
fp.close()
img.add_header('Content-ID', '<att1>')
msg.attach(img)
fp = open(att2, 'rb')
img = MIMEImage(fp.read())
fp.close()
img.add_header('Content-ID', '<{}>'.format(att2))
msg.attach(img)
fp = open(att3, 'rb')
img = MIMEImage(fp.read())
fp.close()
img.add_header('Content-ID', '<{}>'.format(att3))
msg.attach(img)
s = smtplib.SMTP_SSL(mySMTP, smtpPORT)
s.login(myUID,myPASS)
s.sendmail(myEml,myRec, msg.as_string())
...这是我的最终代码,基于 所有工作良好的解决方案对 msgText 进行了一些小的调整!谢谢
def sndFile1():
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
att1 = path + 'df.html'
att2 = path + 'Indices.png'
att3 = path + 'Segments.png'
subject = 'Market Update'
body = 'This Weeks Report'
msg = MIMEMultipart()
msg["To"] = myEml
msg["From"] = myEml
msg["Subject"] = subject
fp = open(att1, 'r')
html = fp.read()
fp.close()
msgText = MIMEText('<b>%s</b><br><%s><br><img src="cid:%s"><br><img src="cid:%s"><br>' % (body, html, att2, att3), 'html')
msg.attach(msgText)
with open(att2, 'rb') as fp:
img = MIMEImage(fp.read())
img.add_header('Content-ID', '<{}>'.format(att2))
msg.attach(img)
with open(att3, 'rb') as fp:
img = MIMEImage(fp.read())
img.add_header('Content-ID', '<{}>'.format(att3))
msg.attach(img)
s = smtplib.SMTP_SSL(mySMTP, smtpPORT)
s.login(myUID,myPASS)
s.sendmail(myEml,myRec, msg.as_string())
s.quit()
解决方案
我修改了你提供的片段。主要变化不是使用 html 数据框作为附件,而是将其插入到消息正文中。
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
with open('df.html', 'w') as fil:
df.head().to_html(fil)
att1 = 'df.html'
att2 = path + 'Indices.png'
att3 = path + 'Segments.png'
subject = 'Market Update'
body = 'This Weeks Report'
msg = MIMEMultipart('alternative')
msg["To"] = myEml
msg["From"] = myEml
msg["Subject"] = subject
with open(att1, 'r') as fp:
html = fp.read()
msgText = MIMEText('<b>%s</b><br>%s<img src="cid:%s"><br><img src="cid:%s"><br>' % (body, html, att2, att3), 'html')
msg.attach(msgText)
with open(att2, 'rb') as fp:
img = MIMEImage(fp.read())
img.add_header('Content-ID', '<{}>'.format(att2))
msg.attach(img)
with open(att3, 'rb') as fp:
img = MIMEImage(fp.read())
img.add_header('Content-ID', '<{}>'.format(att3))
msg.attach(img)
s = smtplib.SMTP_SSL(mySMTP, smtpPORT)
s.ehlo()
s.starttls()
s.login(username,password)
s.sendmail(me,you, msg.as_string())
s.quit()
推荐阅读
- google-cloud-platform - 如何使用 pyspark writestream 写入存储谷歌云?
- javascript - 动态创建具有不同类的跨度
- html - 为什么百分比 `max-width` 在 `img` 上的行为与 `div` 等其他元素(在 flex/grid 容器内)不同?
- django - 在 Django 中访问嵌套值
- thymeleaf - th:这两个例子有什么区别
- node.js - Firebase 云功能本地代码更改未反映在模拟器中
- python - 访问父属性时 Python 子类中的 AttributeError
- python - Keras:如果我在训练几个 epoch 后重新编译我的模型怎么办
- r - 按 R 中的名称从矩阵中删除列
- python - Heroku Worker 没有出现