发送带有 pandas 数据框作为附件的电子邮件 [英] send email with a pandas dataframe as attachment

查看:122
本文介绍了发送带有 pandas 数据框作为附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框,希望将其转换为xlsx并附加到电子邮件中.我可以根据前景发送电子邮件(这是我唯一的方式).我能够将数据帧转换为xlsx并将其保存在驱动器中,然后将其作为附件发送,但我希望直接将其附加,而不必将其保存在驱动器中.看下面我的发送电子邮件的功能:

I have a pandas dataframe that I am looking to convert into an xlsx and attach to an email. I can send emails based on outlook(this is the only way I can do it). I am able to convert the dataframe into an xlsx and save it on my drive and afterwards send it as attachment but I am looking to attach it directly without having to save it on my drive. see bellow my function to send emails:

def email():
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject ="FRANCE SO"
newMail.Body =' '
newMail.To = "email adress"
newMail.Attachments.Add(attachment)
newMail.Send()
return

附件是已转换为xlsx的数据框

attachment is the dataframe that has been transformed into xlsx

推荐答案

您是否尝试过使用Python 3中的 io 模块?它允许您将流用作类似文件的对象,以便期望文件的API可以读取流或将其内容保存到流中.

Have you tried to play with the io module in Python 3? It allows you to use streams as file-like objects, so that APIs that expect a file can read from or save their content to the stream instead.

结合使用StringIO pandas.DataFrame.to_csv :

That works nicely, using a StringIO along with pandas.DataFrame.to_csv:

import io

def export_csv(df):
  with io.StringIO() as buffer:
    df.to_csv(buffer)
    return buffer.getvalue()

之所以可行,是因为to_csv需要一个字符串(解释为路径)或文件句柄,而StringIO可以像文件句柄一样使用.不幸的是, pandas.DataFrame.to_excel 可以与字符串(解释为路径)ExcelWriter .在这种情况下,我们需要自己创建ExcelWriter,并用它包装BytesIO.

That works, because to_csv expects a string (interpreted as a path) or a file handle, and StringIO can be used like a file handle. Unfortunately, pandas.DataFrame.to_excel works with either a string (interpreted as a path) or an ExcelWriter. In that case, we need to create the ExcelWriter ourselves, and wrap a BytesIO with it.

import io
import pandas as pd

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

我不熟悉用于发送电子邮件的Outlook Python工具,我使用SMTP:

I am not familiar with the Outlook Python tools for sending emails, I use SMTP:

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

SEND_FROM = 'noreply@example.com'
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'] = send_to
  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 hope this helps, good luck!

这篇关于发送带有 pandas 数据框作为附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆