import win32com.client
import os
Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder(6)
Filter = "[Subject] = 'John Doe Test Results'"
Items = Inbox.Items.Restrict(Filter)
Item = Items.GetFirst()
messages = Inbox.Items
print(type(messages))
for attachment in Item.Attachments:
print(attachment.FileName)
attachment.SaveAsFile(os.path.join(r"C:UsersConradDesktoptestNew folder" + attachment.FileName))
这个下载电子邮件的附件,并把它放在我桌面上的一个文件夹里。我希望能够打印电子邮件的正文,以便进一步我可以将其添加到数据框并将其发送到excel,但我只需要帮助拉出和打印正文信息。
可以遍历消息并打印每条消息的正文:
import win32com.client
import os
Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder(6)
Filter = "[Subject] = 'John Doe Test Results'"
Items = Inbox.Items.Restrict(Filter)
Item = Items.GetFirst()
messages = Inbox.Items
#NEW CODE
for message in messages:
print(message.body)
print(type(messages))
for attachment in Item.Attachments:
print(attachment.FileName)
attachment.SaveAsFile(os.path.join(r"C:UsersConradDesktoptestNew folder" + attachment.FileName))