Pywin32 不保存来自 Outlook 的附件 (Restrict.Attachements)
Pywin32 not saving attachments from Outlook (Restrict.Attachements)
我正在尝试从 python 的 Outlook 收件箱中的特定发件人下载附件。我正在使用 win32com.client。似乎 python 找到了我正在寻找的电子邮件,但我在尝试保存时收到错误 Restrict.Attachements。有人可以帮忙吗?谢谢
Python: 3.6.12 64 位
import win32com.client as clt
import os
from datetime import datetime, timedelta
outlook = clt.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
inbox = mapi.GetDefaultFolder(6)
messages = inbox.Items
messages = messages.Restrict("[SenderEmailAddress] = testmail@testemail.com")
received_dt = datetime.now() - timedelta(hours=5)
received_dt = received_dt.strftime('%m/%d/%Y %H:%M %p')
for message in list(messages):
try:
s = message.sender
for attachement in messages.Attachments:
attachement.SaveASFile(os.pah.join("C:/Users/me/Desktop/", attachement.FileName))
except Exception as e:
print(e)
编辑:我确认代码找到了正确的电子邮件,但在循环中它经历了异常
>>> for message in list(messages):
... try:
... s = message.sender
... for attachement in messages.Attachments:
... attachement.SaveASFile(os.pah.join("C:/Users/me/Desktop/", attachement.FileName))
... except Exception as e:
... print(message.Subject, "Error: ", e)
...
TestMail Error: Restrict.Attachments
您需要使用 message
对象来获取附件,而不是 messages
对象。在代码中,我看到以下几行:
for attachement in messages.Attachments:
attachement.SaveASFile(os.pah.join("C:/Users/me/Desktop/", attachement.FileName))
相反,您需要使用以下代码:
for attachement in message.Attachments:
attachement.SaveASFile(os.pah.join("C:/Users/me/Desktop/", attachement.FileName))
我正在尝试从 python 的 Outlook 收件箱中的特定发件人下载附件。我正在使用 win32com.client。似乎 python 找到了我正在寻找的电子邮件,但我在尝试保存时收到错误 Restrict.Attachements。有人可以帮忙吗?谢谢
Python: 3.6.12 64 位
import win32com.client as clt
import os
from datetime import datetime, timedelta
outlook = clt.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
inbox = mapi.GetDefaultFolder(6)
messages = inbox.Items
messages = messages.Restrict("[SenderEmailAddress] = testmail@testemail.com")
received_dt = datetime.now() - timedelta(hours=5)
received_dt = received_dt.strftime('%m/%d/%Y %H:%M %p')
for message in list(messages):
try:
s = message.sender
for attachement in messages.Attachments:
attachement.SaveASFile(os.pah.join("C:/Users/me/Desktop/", attachement.FileName))
except Exception as e:
print(e)
编辑:我确认代码找到了正确的电子邮件,但在循环中它经历了异常
>>> for message in list(messages):
... try:
... s = message.sender
... for attachement in messages.Attachments:
... attachement.SaveASFile(os.pah.join("C:/Users/me/Desktop/", attachement.FileName))
... except Exception as e:
... print(message.Subject, "Error: ", e)
...
TestMail Error: Restrict.Attachments
您需要使用 message
对象来获取附件,而不是 messages
对象。在代码中,我看到以下几行:
for attachement in messages.Attachments:
attachement.SaveASFile(os.pah.join("C:/Users/me/Desktop/", attachement.FileName))
相反,您需要使用以下代码:
for attachement in message.Attachments:
attachement.SaveASFile(os.pah.join("C:/Users/me/Desktop/", attachement.FileName))