有数千封电子邮件,我希望在运行脚本时将一封主题行固定的特定电子邮件的附件保存到当前工作目录中。
我已经尝试阅读一些有关此的文章,其中之一是-How to save attachment from outlook using win32com.client in Python?
Another reddit thread about same thing, but I want for only one particular email
from win32com.client import Dispatch
import datetime as date
import os
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
val_date = date.date.today()
sub_today = 'Download Attachment 1'
att_today = 'Attachment 1.pdf'
for msg in all_inbox:
if msg.Subject == sub_today:
break
for att in msg.Attachments:
if att.FileName == att_today:
break
att.SaveAsFile(os.getcwd() + '\\Attachment 1.pdf')
att.ExtractFile('Attachment 1.pdf')
open(att)
att.WriteToFile('x')
这是我运行脚本时遇到的错误:
D:\r>python attach.py
Traceback (most recent call last):
File "attach.py", line 22, in <module>
open(att)
TypeError: expected str, bytes or os.PathLike object, not CDispatch
我希望python将附件从最新的电子邮件保存到目录中。