如何使用 python 从 outlook 提取电子邮件正文并将其解析为 pandas df?

How do I pull and parse the body of an email from outlook with python into a pandas df?

我正在尝试将电子邮件正文从 Outlook 提取到 pandas 数据框中。如何将 msg.Body 分解为可以写入 csv 并摄取到 pandas 中的行?

这是我目前所拥有的(我可以使用它打印到屏幕并复制并粘贴到 Excel 以进行更多操作):

import win32com.client
import win32com

outlook = win32com.client.Dispatch('Outlook.Application').GetNameSpace('MAPI')
FedEx_Claims = outlook.GetDefaultFolder(6).Folders['FedExClaims']

for msg in FedEx_Claims.Items:
    body = msg.Body
    print(body)

我试过将正文强制转换为字符串,但初步测试并未表明它被视为字符串,因为我无法过滤以仅包含其中带有“:”的行。

s_msg = str(body)
for line in s_msg:
    if ':' in line:
        print(s_msg, end='')

如有任何帮助,我们将不胜感激。

注意:我目前无法访问 IMAP,但我正在询问我的 IT 是否可以更改。

https://pypi.org/project/exchangelib/

使用 exchangelib
from exchangelib import DELEGATE, Account, Credentials, Configuration

creds = Credentials(
    username='X', password='X'
)

config = Configuration(server='X.ca', credentials=creds)

account = Account(
    primary_smtp_address="X@gmail.com",
    autodiscover=False, 
    config=config,
    access_type=DELEGATE
)

for item in account.inbox.all().order_by('-datetime_received')[:1]:
    print(item.body)

item.body的内容会在html,然后用pandas.read_html到read/structure的内容

https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.read_html.html