有没有办法使用 Python Win32com 模块读取任何本地 PST 文件并将消息导出为 MSG 文件? [英] Is there a way to use Python Win32com module to read any local PST file and export messages as MSG files?

查看:40
本文介绍了有没有办法使用 Python Win32com 模块读取任何本地 PST 文件并将消息导出为 MSG 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a two stages Digital Forensics project, and on stage one, I need to extract all the messages stored on several outlook's PST/OST files, and save them as MSG files in a folder hierarchy like pstFilename\inbox, draft, sent... for each PST file in the sample.

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

For stage two, now completed, I am using python (3.x) and the Win32Com module to traverse all subfolder inside the target folder, search and hash every MSG file, parse a number of MSG properties and finally, create a CSV report. I found plenty of documentation and code samples to parse a MSG file using python and the Win32Com module, but not so much on how to parse a single PST file other than the PST file associated to Outlook's user profile on the local computer.

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

I am looking for a way to open a PST file using the win32Com module, traverse all folders in it, and export/save every message as a MSG file to the corresponding pstfilename_folder\subfolder.

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

There is a very straightforward method to access MSG files:


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 文件?

Is there any equivalent method to access and manipulate a PST file using the win32com module?

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

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

but I not sure how to use it in python...

推荐答案

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

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/d4c0f9a1d21a0b29a7fs
  2. https://github.com/matthewproctor/OutlookAttachmentExtractor
  3. https://docs.microsoft.com/en-us/office/vba/api/outlook.namespace

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

My solution doesn't save the .msg files as you request in your question, but unless you have a secondary use for outputting the files this solution should save you a step.

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 Win32com 模块读取任何本地 PST 文件并将消息导出为 MSG 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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