Python 在电子邮件中附加excel文件(XLSX)
我正在尝试在电子邮件中附加XLSX文件。我可以发送XLSX文件并使用谷歌的内部excel查看它,但当我下载该文件时,它不会另存为XLSX文件 下面是我附加XLSX文件的部分的一个片段:Python 在电子邮件中附加excel文件(XLSX),python,excel,python-3.x,gmail,Python,Excel,Python 3.x,Gmail,我正在尝试在电子邮件中附加XLSX文件。我可以发送XLSX文件并使用谷歌的内部excel查看它,但当我下载该文件时,它不会另存为XLSX文件 下面是我附加XLSX文件的部分的一个片段: path = os.listdir('C:\\Users\\Myuser\\Desktop\\Excelfiles') output = [i for i in path][-1] filename = 'C:\\Users\\MyUser\\Desktop\\Excelfiles\\{0}'.format(o
path = os.listdir('C:\\Users\\Myuser\\Desktop\\Excelfiles')
output = [i for i in path][-1]
filename = 'C:\\Users\\MyUser\\Desktop\\Excelfiles\\{0}'.format(output)
content_type,encoding = mimetypes.guess_type(filename)
main_type, sub_type = content_type.split('/', 1)
fp = open(filename,'rb')
att = MIMEApplication(fp.read(), sub_type)
fp.close()
att.add_header('Content-Disposition','attachment',filename="Test")
msg.attach(att)
谢谢 尝试更改此行:
att.add_header('Content-Disposition','attachment',filename="Test")
致:
由于需要在文件名末尾添加
.xlsx
。@dko512已编辑,请检查:-)
att.add_header('Content-Disposition','attachment',filename="Test.xlsx")