使用python 3在电子邮件中嵌入Pandas df html表

时间:2018-08-07 11:45:33

标签: python html-table embed html-email

我正在尝试嵌入和发送用pandas .to_html创建的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()

1 个答案:

答案 0 :(得分:4)

我修改了您提供的代码段。主要更改不是使用html数据框作为附件,而是将其插入消息正文中。

public interface IMyHubClient
{
    void Ping();
}
 public class MyHub : Hub<IMyHubClient>
{
    ...
}