python - 从 .PST 文件中提取文本

标签 python pst

我正在尝试提取 .pst 文件的内容(作为字符串/文本)。

我尝试了不同的答案,但没有找到任何相关的解决方案。

Outlook PST File Parsing in Python

Read PST files from win32 or pypff

Export PST and OST with pypff / libpff

我主要关注库 libpff ( https://github.com/libyal/libpff ),但我认为该库对于提取 pst 文本没有帮助。

我的代码:

import pypff
pst = pypff.file()
pst.open("my_pst_file.pst")

代码打开 pst,但我不知道如何将其内容提取为 txt。

最佳答案

是的,您可以使用pypff来提取文本。我也点击了这个链接(Export PST and OST with pypff / libpff)。

pypff.file() 可能会令人困惑,因为开发人员没有提供每个函数和属性的合适文档作为说明。我花了一段时间自己探索。

这是我最近所做的。

# path to your pst file
opst = pypff.open(path)
root = opst.get_root_folder()

# 3 subfolders, for me, only 2nd one has content
# Use 'root.get_number_of_sub_folders()' to see which folder is blank
folder = root.get_sub_folder(1)
# 2 subfolders, the 2nd one is my inbox
inbox = folder.get_sub_folder(1)

# mail count in current folder
count = inbox.get_number_of_sub_items()

# Example of extracting info from one email
msg = inbox.get_sub_item(0)

subject = msg.subject
content = msg.plain_text_body.decode()
sender = msg.sender_name
header = msg.transport_headers
sent_time = msg.delivery_time

if msg.number_of_attachments > 0:
    # read from attachment 1
    size = attachment = msg.get_attachment(0).get_size()
    attachment_content = (msg.get_attachment(0).read_buffer(attach_size)).decode('ascii', errors='ignore')

对于那些想要使用pypff的人,不要使用pip install。它仅从版本 20161119 开始构建,这对我来说经常崩溃。

从新版本构建 website 。有一个 setup.py,它应该很容易构建。

对于附件,ascii 解码器并不理想。我已经尝试过all 98 decoders在python3中,没有人可以解码每个字节。这意味着,单一方法无法解码所有内容。就我而言,utf_16 可以提取内容,这对我来说已经足够了。

关于python - 从 .PST 文件中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58507417/

相关文章:

java - 如何从 pst 文件获取帖子附件

markov-chains - "e"出现在概率后缀树序列中的哪个位置?

python - 检查python中矩阵中接近元素的值

Python:如何将 'exec' 与自定义范围一起使用?

Python 删除空格

c# - 检查 pst 文件是否受密码保护

java - 在java中创建一个电子邮件对象并将其保存到文件

Python - 根据前者的键值将字典的键作为另一个字典的键值附加

python - 给定一个带有元组键的 python 字典,正确格式化字符串

linux - 基于 Linux 的解决方案初学者指南,用于从 MS Outlook 的 .pst 文件中读取邮件内容(包括附件)