【问题标题】:Read PST files from win32 or pypff从 win32 或 pypff 读取 PST 文件
【发布时间】:2019-06-10 12:58:46
【问题描述】:

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

使用 win32,我们可以使用以下方式启动 Outlook 对象:

import win32com.client

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

GetDefaultFolder(6) 获取收件箱文件夹。然后我可以使用这个文件夹的函数和属性来使用。

但我想要的是提供我自己的 pst 文件,pywin32(或任何其他库)可以读取。在这里它只连接到我的 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()

但我想要消息大小等属性,还想要访问 pypff 中不可用的 pst 文件中的日历(我不知道)

问题

  1. 如何读取 PST 文件以获取电子邮件大小、附件类型和日历等数据?
  2. 有可能吗? win32pypff 或任何其他库中是否有解决方法?

【问题讨论】:

    标签: python pywin32 win32com pst


    【解决方案1】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2011-03-14
      • 2020-02-18
      • 2012-05-26
      相关资源
      最近更新 更多