Nic*_*osh 4 email python-3.x pandas
举个例子:
df_1 = ([1,2,3,5])
df_2 = ([10,20,30,50])
df_test =pd.concat([pd.DataFrame(df_1),pd.DataFrame(df_2)],axis=1)
如何通过gmail发送带有此数据框的电子邮件,使其看起来像表格?
这是我尝试的:
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr , ".......")
msg = df_test.to_html()
server.sendmail(fromaddr, toaddr, msg)
server.quit()
尝试:
str.format附加的DF HTML电子邮件主体的HTML。例如:
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import smtplib
import sys
recipients = ['ToEmail@domain.com'] 
emaillist = [elem.strip().split(',') for elem in recipients]
msg = MIMEMultipart()
msg['Subject'] = "Your Subject"
msg['From'] = 'from@domain.com'
html = """\
<html>
  <head></head>
  <body>
    {0}
  </body>
</html>
""".format(df_test.to_html())
part1 = MIMEText(html, 'html')
msg.attach(part1)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.sendmail(msg['From'], emaillist , msg.as_string())
安装 Pretty-html-table
from pretty_html_table import build_table
body = """
<html>
<head>
</head>
<body>
        {0}
</body>
</html>
""".format(build_table(df, 'blue_light'))
您不必担心格式,而且如果您的 DataFrame 中有网站链接,那么输出将仅包含超链接。
| 归档时间: | 
 | 
| 查看次数: | 3871 次 | 
| 最近记录: |