Home > other >  Send a .xlsx via python
Send a .xlsx via python

Time:06-21

I can send mail with a csv with this code:

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

def send_email(send_to, subject, df):
    send_from = "[email protected]"
    password = "xxx"
    message = """\
    <p><strong>This is a test email&nbsp;</strong></p>
    <p><br></p>
    <p><strong>Greetings&nbsp;</strong><br><strong>Alexandre&nbsp;</strong></p>
    """

    multipart = MIMEMultipart()
    multipart["From"] = send_from
    multipart["To"] = send_to
    multipart["Subject"] = subject  
    attachment = MIMEApplication(df.to_csv())
    attachment["Content-Disposition"] = 'attachment; filename=" {}"'.format(f"{subject}.csv")
    multipart.attach(attachment)
    multipart.attach(MIMEText(message, "html"))
    server = smtplib.SMTP("smtp-mail.outlook.com", 587)
    server.starttls()
    server.login(multipart["From"], password)
    server.sendmail(multipart["From"], multipart["To"], multipart.as_string())
    server.quit()
        
send_email("[email protected]", "Covid-19 data per country", ListeProjetsCodeNovaMergefinal)

But if a replace df.to_csv() with df.to_excel() and .csv with .xlsx i have an error:

TypeError: to_excel() missing 1 required positional argument: 'excel_writer'

Do you have any idea?

CodePudding user response:

To write a single object to an Excel .xlsx file it is only necessary to specify a target file name.

# storing into the excel file
data.to_excel("output.xlsx")
  • Related