【问题标题】:How do I pull and parse the body of an email from outlook with python into a pandas df?如何使用 python 从 Outlook 中提取电子邮件正文并将其解析为 pandas df?
【发布时间】:2020-12-30 07:00:19
【问题描述】:

我正在尝试将电子邮件正文从 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 是否可以更改。

【问题讨论】:

  • print( body )print( type(body) ) 得到什么?

标签: python pandas email outlook win32com


【解决方案1】:

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 读取/构造内容

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

【讨论】:

    猜你喜欢
    • 2018-11-10
    • 1970-01-01
    • 2011-08-24
    • 2019-01-09
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    相关资源
    最近更新 更多