python - 有没有办法使用 Python Win32com 模块读取任何本地 PST 文件并将消息导出为 MSG 文件?

标签 python outlook win32com msg pst

我正在开展一个两阶段的数字取证项目,在第一阶段,我需要提取存储在多个 Outlook 的 PST/OST 文件中的所有消息,并将它们作为 MSG 文件保存在文件夹层次结构中,例如 pstFilename\inbox,对于示例中的每个 PST 文件,草稿、发送...。

对于现已完成的第二阶段,我使用 python (3.x) 和 Win32Com 模块遍历目标文件夹内的所有子文件夹,搜索并散列每个 MSG 文件,解析许多 MSG 属性,最后创建一个CSV 报告。我找到了大量使用 python 和 Win32Com 模块解析 MSG 文件的文档和代码示例,但没有太多关于如何解析与本地计算机上 Outlook 用户配置文件关联的 PST 文件之外的单个 PST 文件的信息。

我正在寻找一种方法来使用 win32Com 模块打开 PST 文件,遍历其中的所有文件夹,并将每条消息作为 MSG 文件导出/保存到相应的 pstfilename_folder\subfolder。

有一种非常简单的方法来访问 MSG 文件:


import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
msg = outlook.OpenSharedItem(r"/test_files/test.msg")

print(msg.SenderName)
print(msg.SenderEmailAddress)
print(msg.SentOn)
print(msg.To)
print(msg.CC)
print(msg.BCC)
print(msg.Subject)
print(msg.Body)

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

del outlook, msg

是否有任何等效方法可以使用 win32com 模块访问和操作 PST 文件?

我找到了这个链接:https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook.store?view=outlook-pia

但我不知道如何在 python 中使用它......

最佳答案

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

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

我的解决方案不会按照您在问题中请求的方式保存 .msg 文件,但除非您有二次用途来输出文件,否则此解决方案应该可以为您节省一个步骤。

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)

关于python - 有没有办法使用 Python Win32com 模块读取任何本地 PST 文件并将消息导出为 MSG 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353989/

相关文章:

python - 为什么 python 在 Ubuntu 和 MacOS 上解析日期的方式不同?

javascript - 通过javascript将参数传递给python函数

python - 在TextInput中单击鼠标时光标消失(不闪烁)-Python Kivy

python - 为什么 win32com 比 xlrd 慢那么多?

python - 如何在 Python 3.4 或 Python 2.7 上安装 win32com.client

python - 使用替换字典替换字符串的最简单方法?

outlook - 从 Outlook 发送到 Gmail 时为 HTML 电子邮件添加的额外空间

HTML 电子邮件格式无法在 MS Outlook 中正确呈现

html - Outlook 签名 valign 在 gmail 中不起作用

python - 为什么这个脚本不能与线程 python 一起使用