[英]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. 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.
This is my code:这是我的代码:
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 does not provide access to the attachment binary data. Outlook Object Model 不提供对附件二进制数据的访问。 The best you can do is save the attachment as a temporary file (
Attachment.SaveAsFile
), read file contents, delete the file.您可以做的最好的是将附件保存为临时文件(
Attachment.SaveAsFile
),读取文件内容,删除文件。
On the MAPI level however (C++ or Delphi only), attachments can only be accessed as blobs or as IStream
(or IStorage
) interface;然而,在 MAPI 级别(仅限 C++ 或 Delphi),附件只能作为 blob 或
IStream
(或IStorage
)接口访问; MAPI knows nothing about files. MAPI 对文件一无所知。 If using Redemption is an option, it provides access to the attachment data through the
AsText
/ AsArray
/ EmbeddedMsg
/ OleStorage
/ AsStream
properties - see https://www.dimastr.com/redemption/RDOAttachment.htm and https://www.dimastr.com/redemption/safe_Attachment.htm如果使用Redemption是一个选项,它通过
AsText
/ AsArray
/ EmbeddedMsg
/ OleStorage
/ AsStream
属性提供对附件数据的访问 - 请参阅https://www.dimastr.com/redemption/RDOAttachment.htm和https://www。 dimastr.com/redemption/safe_Attachment.htm
any way to read the attachments without downloading them
无需下载即可阅读附件的任何方式
No. You need to save the file on the disk first.不可以。您需要先将文件保存在磁盘上。 Then you can open the just saved file on the disk and process it in your application.
然后您可以打开磁盘上刚刚保存的文件并在您的应用程序中处理它。
# 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.