在电子邮件中附加Excel文件(XLSX)

时间:2020-01-24 02:14:20

标签: python excel python-3.x gmail

我正在尝试在电子邮件中附加XLSX文件。我可以发送XLSX文件并使用Google内部的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(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)

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试更改此行:

att.add_header('Content-Disposition','attachment',filename="Test")

收件人:

att.add_header('Content-Disposition','attachment',filename="Test.xlsx")

因为您需要在文件名的末尾添加.xlsx