如何在没有签名的情况下从 outlook 打印电子邮件正文 - Python

How to print email body from outlook without signature - Python

我正在尝试解析来自 Outlook 的电子邮件。 我想要打印以下内容:

有什么方法可以在多space行之间打印出正文(通常这是将签名与正文分开的方式)?

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

import win32com.client
#other libraries to be used in this script
import os
from datetime import datetime, timedelta


outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")

 
for account in mapi.Accounts:
    print(account.DeliveryStore.DisplayName) 
    
    
inbox = mapi.GetDefaultFolder(6)


messages = inbox.Items
messages.Sort('[ReceivedTime]', True)
received_dt = datetime.now() - timedelta(days=1)
received_dt = received_dt.strftime('%m/%d/%Y %H:%M %p')
messages = messages.Restrict("[ReceivedTime] >= '" + received_dt + "'")
messages = messages.Restrict("[SenderEmailAddress] = 'firstname.lastname@gmail.com'")
message = messages.GetFirst()

print ("Current date/time: "+ received_dt)
while message:
    print(message.Subject)
    print(message.body)
    message = messages.GetNext ()

您可以使用正则表达式忽略三个换行符后的所有内容(段落之间通常有一个或两个换行符):

import re

r = re.compile(r"(.*)\n\n\n", re.MULTILINE + re.DOTALL)

# ...

while message:
    # ...
    match = r.match(message.body)
    if match:
        body_without_signature = r.match(message.body).groups(0)
    else:
        # No signature found
        body_without_signature = message.body
    print(body_without_signature)