从win32或pypff读取PST文件

问题描述 投票:2回答:1

我想使用Python读取PST文件。我已经找到2个库win32和pypff

使用win32,我们可以使用以下命令初始化一个Outlook对象:

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)

GetDefaultFolder(6)获取收件箱文件夹。然后,我可以使用此文件夹的功能和属性来使用它。

但是我想要提供我自己的pywin32(或任何其他库)可以读取的pst文件。在这里它仅与我的Outlook应用程序连接

使用pypff,我可以使用以下代码处理pst文件:

import pypff
pst_file = pypff.file()
pst_file.open('test.pst')

root = pst_file.get_root_folder()

for folder in root.sub_folders:
    for sub in folder.sub_folders:
        for message in sub.sub_messages:
            print(message.get_plain_text_body()

但是我想要属性,例如消息的大小,还希望访问pst文件中的日历,而这些日历在pypff中不可用(我不知道)

问题

  1. 如何读取PST文件以获取诸如电子邮件大小,其附件类型和日历之类的数据?
  2. 有可能吗? win32pypff或任何其他库中都有解决方法吗?
python pywin32 win32com pst
1个回答
0
投票

这是我想为自己的应用程序做的事情。我能够从这些来源整理出一个解决方案:

  1. https://gist.github.com/attibalazs/d4c0f9a1d21a0b24ff375690fbb9f9a7
  2. https://github.com/matthewproctor/OutlookAttachmentExtractor
  3. https://docs.microsoft.com/en-us/office/vba/api/outlook.namespace

上面的第三个链接应提供有关可用属性和各种项目类型的更多详细信息。我的解决方案仍然需要连接到Outlook应用程序,但是对用户来说应该是透明的,因为在try / catch / finally块中使用pst存储区会自动将其删除。我希望这可以帮助您走上正确的轨道!

import win32com.client

def find_pst_folder(OutlookObj, pst_filepath) :
    for Store in OutlookObj.Stores :
        if Store.IsDataFileStore and Store.FilePath == pst_filepath :
            return Store.GetRootFolder()
    return None

def enumerate_folders(FolderObj) :
    for ChildFolder in FolderObj.Folders :
        enumerate_folders(ChildFolder)
    iterate_messages(FolderObj)

def iterate_messages(FolderObj) :
    for item in FolderObj.Items :
        print("***************************************")
        print(item.SenderName)
        print(item.SenderEmailAddress)
        print(item.SentOn)
        print(item.To)
        print(item.CC)
        print(item.BCC)
        print(item.Subject)

        count_attachments = item.Attachments.Count
        if count_attachments > 0 :
            for att in range(count_attachments) :
                print(item.Attachments.Item(att + 1).Filename)

Outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

pst = r"C:\Users\Joe\Your\PST\Path\example.pst"
Outlook.AddStore(pst)
PSTFolderObj = find_pst_folder(Outlook,pst)
try :
    enumerate_folders(PSTFolderObj)
except Exception as exc :
    print(exc)
finally :
    Outlook.RemoveStore(PSTFolderObj)
© www.soinside.com 2019 - 2024. All rights reserved.