Skip to content Skip to sidebar Skip to footer

How Attach Pandas Dataframe As Excel In Email Triggered From Python

I have a pandas dataframe which I want attach as xls in an automated email triggered from python. How can this be done I am able to send the email without attachment successfully b

Solution 1:

As pointed out in comment you are not attaching the file, so it would not be sent.

msg.attach(MIMEText(body, 'plain'))
filename = "Your file name.xlsx"
attachment = open("/path/to/file/Your file name.xlsx","rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename=%s" % filename)

msg.attach(part)
text = msg.as_string()
smtp0bj.sendmail(msg['From'], msg['To'], text)

Hope it helps

Post a Comment for "How Attach Pandas Dataframe As Excel In Email Triggered From Python"