从win32或pypff读取PST文件 [英] Read PST files from win32 or pypff

查看:203
本文介绍了从win32或pypff读取PST文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python读取PST文件.我发现2个库win32和pypff

I want to read PST files using Python. I've found 2 libraries win32 and pypff

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

Using win32 we can initiate a outlook object using:

import win32com.client

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

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

The GetDefaultFolder(6) gets the inbox folder. And then I can use this folders functions and attribute to work with.

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

But what I want is to give my own pst files which pywin32(or any other library) can read. Here it only connects with my Outlook Application

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

With pypff I can use the below code to work with pst files:

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()

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

But I want attributes like the size of the message and also like to access calendars in the pst files which is not available in pypff(not that I know of)

问题

  1. 如何读取PST文件以获取诸如电子邮件大小,附件类型和日历之类的数据?
  2. 有可能吗? win32pypff或任何其他库中是否有变通办法?
  1. How can I read PST files to get data like the size of the email, the types of attachments it has and the calendars?
  2. Is it possible? Is there a work around in win32, pypff or any other library?

推荐答案

这是我要为自己的应用程序执行的操作.我能够从以下来源整理出一个解决方案:

This is something that I want to do for my own application. I was able to piece together a solution from these sources:

  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
  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存储区将其自动删除.我希望这可以帮助您走上正确的轨道!

The third link above should give additional details about available attributes and various item types. My solution still needs to connect to your Outlook application, but it should be transparent to the user since the pst store is automatically removed using in the try/catch/finally block. I hope this helps you get on the right track!

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)

这篇关于从win32或pypff读取PST文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆