python - 如何将 Python 数据帧存储在内存中并将其作为 Excel 附件发送到特定电子邮件地址?

标签 python excel smtp mime mimemultipart

我从 mongo dB 读取数据并创建报告。我曾经将报告保存为本地计算机上的 .xlsx 文件,并使用电子邮件手动发送。我想自动化这个过程。

我在 Python 3 中找到了一种使用 Python 的 io.BytesIO() 功能将文件保存在内存中的方法。我尝试了一些在堆栈溢出上找到的东西,但它对我不起作用。我想获得一些有关如何自动化工作的建议。

以下是我尝试过的,

df = pd.dataframe(df) # has my reports dataframe

def export_excel(df):
    with io.BytesIO() as buffer:
        writer = pd.ExcelWriter(buffer)
        df.to_excel(writer)
        writer.save()
        return buffer.getvalue()

from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

SEND_FROM = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2845514d45494144684b474558494651064b4745" rel="noreferrer noopener nofollow">[email protected]</a>'
EXPORTERS = {'dataframe.csv': export_csv, 'dataframe.xlsx': export_excel}

def send_dataframe(send_to, subject, body, df):
    multipart = MIMEMultipart()
    multipart['From'] = SEND_FROM
    multipart['To'] = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="177a6e727a767e7b5774787a6776796e3974787a" rel="noreferrer noopener nofollow">[email protected]</a>'
    multipart['Subject'] = subject
    for filename in EXPORTERS:    
        attachment = MIMEApplication(EXPORTERS[filename](df))
        attachment['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
        multipart.attach(attachment)
    multipart.attach(MIMEText(body, 'html'))
    s = smtplib.SMTP('localhost')
    s.sendmail(SEND_FROM, send_to, multipart.as_string())
    s.quit()

I do not get an error but don't get an email with an excel attachment too.

最佳答案

我需要解决同一主题,所以这是我的代码:

import io
import pandas as pd 
from pandas import util

from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

df = util.testing.makeDataFrame()
# df.head()

output = io.BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
output.seek(0)

send_from = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="402d391f272d21292c00272d21292c6e232f2d" rel="noreferrer noopener nofollow">[email protected]</a>'
gmail_pwd = 'my_gmail_password'
send_to = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b4652744c464a42476b4c464a424705484446" rel="noreferrer noopener nofollow">[email protected]</a>'
subject = 'Daily report'
body = "<p>Please find attached report,<br/>by me</p>"
report_name = "report.xlsx"

msg = MIMEMultipart()
msg['Subject'] = subject  # add in the subject
msg['From'] = send_from
msg['To'] = send_to
msg.attach(MIMEText(body, 'html'))  # add text contents
file = MIMEApplication(output.read(), name=report_name)
file['Content-Disposition'] = f'attachment; filename="{report_name}"'
msg.attach(file)

smtp = smtplib.SMTP('smtp.gmail.com:587')
smtp.ehlo()
smtp.starttls()
smtp.login(send_from, gmail_pwd)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.quit()

关于python - 如何将 Python 数据帧存储在内存中并将其作为 Excel 附件发送到特定电子邮件地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64216843/

相关文章:

excel - 强制 Excel 退出编辑模式的宏

c# - smtp.gmail.com 异常,启用发送邮件

在不太了解服务器配置的情况下发送电子邮件的 C# 代码?

python - config.from_object 在使用 Python 3 的 Flask 中不起作用

python - sklearn断言错误: not equal to tolerance on custom estimator

python - Tensorflow Lite,图像大小零误差

arrays - 使用 VBA 将单元格值范围分配给变量数组

python - PyCrypto 返回 AES 的 key 大小错误

excel - 当存在 double.NaN 时 SpreadsheetGear SetArray of double

python - 通过本地Exim MTA用python发送电子邮件