0

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 = "xxx.xxx@hotmail.fr"
    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("xxx.xxx@hotmail.fr", "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?

Rocheteau
  • 43
  • 7
  • Does this answer your question? [TypeError: to\_excel() missing 1 required positional argument - despite using excel writer](https://stackoverflow.com/questions/51597773/typeerror-to-excel-missing-1-required-positional-argument-despite-using-exc) – Peter Jun 20 '22 at 12:03
  • No i am already in df.to_excel() – Rocheteau Jun 20 '22 at 12:08

1 Answers1

0

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")
Ola Galal
  • 162
  • 1
  • 11