[英]Get Email list from Outlook using Python
我正在嘗試獲取我在 outlook 中收到的最后 1000 封電子郵件。 但是代碼只從主文件夾而不是從子文件夾中獲取 Email。 請協助
import win32com.client
import pandas as pd
import dateutil.parser
from datetime import datetime
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
# the inbox. You can change that number to reference
# any other folder
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)
i=1
df = pd.DataFrame(columns=['Sender','Subject','DateTime'])
Today = datetime.now().strftime("%m/%d/%Y") # current date and time
while i<1000:
message=messages[i]
DT1=message.ReceivedTime
DT = DT1.strftime("%m/%d/%Y, %H:%M:%S")
a=message.SenderEmailAddress
if "-" in a:
a=a.split("-",1)[1]
b=message.subject
df = df.append({'Sender':a,'Subject':b,'DateTime':DT}, ignore_index=True)
i+=1
df.to_excel("C:/Users/abc/Downloads/Email.xlsx")
要對多個文件夾執行搜索,您需要使用Application
class 的AdvancedSearch
方法。 在 Outlook 中使用AdvancedSearch
方法的主要好處是:
AdvancedSearch
方法會在后台自動運行它。Restrict
和Find
/ FindNext
方法可以應用於特定的Items
集合。Store
類的IsInstantSearchEnabled
屬性)。 閱讀有關AdvancedSearch
方法的更多信息並在 Outlook 中以編程方式查找示例:C#、VB.NET文章。
Items
class 的Restrict
或Find
/ FindNext
方法僅允許根據您的條件從單個文件夾中獲取項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.