使用 python 3 在电子邮件中嵌入 Pandas df html table
Embed Pandas df html table in email using python 3
我正在尝试嵌入并发送使用 pandas
.to_html
创建的 html table。
我很乐意将 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 数据发送到电子邮件,但作为未格式化的文本发送,并且似乎无法通过格式化(即)table 和简单的边框来解决这种方法。
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 tables.
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()
我正在尝试嵌入并发送使用 pandas
.to_html
创建的 html table。
我很乐意将 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 数据发送到电子邮件,但作为未格式化的文本发送,并且似乎无法通过格式化(即)table 和简单的边框来解决这种方法。
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 tables.
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()