簡體   English   中英

如何在沒有簽名的情況下從 outlook 打印 email 正文 - Python

[英]How to print email body from outlook without signature - Python

我正在嘗試解析來自 Outlook 的電子郵件。 我想打印以下內容:

  • 主題
  • 正文(不包括發件人的簽名)
  • 忽略之前所有來自轉換的電子郵件(回復和轉發)

有什么辦法可以在行之間的多空格之前打印出正文(通常這是簽名與正文分開的方式)?

任何幫助,將不勝感激!

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM