我正在尝试使用 win32com.client 来打印 outlook 电子邮件的正文消息,但无法弄清楚。如果有人能帮忙,那就太棒了

Im trying to use win32com.client to print the body message of an outlook email but cannot figure it out. If anyone could help thatd be amazing

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:\Users\Conrad\Desktop\test\New 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:\Users\Conrad\Desktop\test\New folder" + attachment.FileName))