attributeerror:“工作簿”对象没有“读取”属性

js4nwp54  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(305)

我正在尝试将excel文件作为电子邮件附件发送,并通过python发送

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
mail_content = '''Hello,
This is a test mail.
In this mail we are sending some attachments.
The mail is sent using Python SMTP library.
Thank You
'''

# The mail addresses and password

msg = MIMEMultipart()
msg['From'] = 'from'
msg['To'] = 'to'
msg['Subject'] = 'this is a mail'
msg.attach(MIMEText('Names'))

part = MIMEBase('application', "octet-stream")
part.set_payload(open("Names.xlsx", "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="Names.xlsx"')
msg.attach(part)

    #context = ssl.SSLContext(ssl.PROTOCOL_SSLv3)
    #SSL connection only working on Python 3+
smtp = smtplib.SMTP('smtp.gmail.com',587)
if smtp:
    smtp.starttls()
smtp.login('xxx','xx')
smtp.sendmail('from', 'to', msg.as_string())
smtp.quit()

但是当我运行这段代码时,我得到了下面的错误
attributeerror:“工作簿”对象没有“读取”属性
我已经安装了所有必需的软件包。请帮我纠正这个错误。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题