【发布时间】:2020-01-12 06:27:05
【问题描述】:
我收到一封包含 xls 文件的批处理电子邮件,我有一个脚本可以在我的 Outlook 收件箱中搜索该电子邮件并提取附件。我想将文件保存为 xlsx 而不是当前格式的 xls。
我试图修改 SaveAsFile 附件方法中的文件名,以在末尾包含 x - attachment.SaveAsFile(os.path.join(file_home_path, new_file_name)+"x") - 这确实将文件保存为xlsx 但文件已损坏,我无法打开它。
还有其他附件方法可以在源头修改文件扩展名吗?
import win32com.client
import os
import datetime
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
now = datetime.datetime.now().strftime("%Y %m %d")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
file_home_path = "C:/Desktop"
for message in messages:
if message.Subject == 'subject_to_search_for':
attachments = message.Attachments
for attachment in attachments:
new_file_name = 'required_file_{}.xls'.format(now)
attachment.SaveAsFile(os.path.join(file_home_path, new_file_name))
break
message.Delete()
【问题讨论】: