簡體   English   中英

嘗試使用 python 讀取 outlook 附件

[英]Trying to read outlook attachments with python

I am working on a script that reads emails from Outlook using Python, I wanted to know if there is any way to read the attachments without downloading them, for example open an attached pdf and capture it in a DataFrame.

這是我的代碼:

import win32com.client
import os

mail ='email_name@gmail.com'
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

stores = outlook.Stores
for store in stores:
        if store.DisplayName == mail:
                inbox = store.GetDefaultFolder(6)
                messages = inbox.Items
                for attachment in messages[1].Attachments:
                        print(attachment)

Outlook Object Model 不提供對附件二進制數據的訪問。 您可以做的最好的是將附件保存為臨時文件( Attachment.SaveAsFile ),讀取文件內容,刪除文件。

然而,在 MAPI 級別(僅限 C++ 或 Delphi),附件只能作為 blob 或IStream (或IStorage )接口訪問; MAPI 對文件一無所知。 如果使用Redemption是一個選項,它通過AsText / AsArray / EmbeddedMsg / OleStorage / AsStream屬性提供對附件數據的訪問 - 請參閱https://www.dimastr.com/redemption/RDOAttachment.htmhttps://www。 dimastr.com/redemption/safe_Attachment.htm

無需下載即可閱讀附件的任何方式

不可以。您需要先將文件保存在磁盤上 然后您可以打開磁盤上剛剛保存的文件並在您的應用程序中處理它。

# Creating an object for the message.Attachments.
attachment = message.Attachments
# To check which item is selected among the attacments.
print (message.Attachments.Item(which_item))
# To iterate through email items using message.Attachments object.
for attachment in message.Attachments:
   # To save the perticular attachment at the desired location in your hard disk.
   attachment.SaveAsFile(os.path.join("D:\Script\Attachments",file_name)) 

暫無
暫無

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

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