libpff-python

20231205 · active · verified Thu Apr 16

libpff-python provides Python bindings for libpff, an open-source library designed to access Personal Folder File (PFF) and Offline Folder File (OFF) formats, commonly used by Microsoft Outlook for storing emails, contacts, and other data. The current version is 20231205. Releases appear to occur annually or every few years, often coinciding with updates to the underlying C library. [2, 1, 3]

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to open a PST file, navigate its folder hierarchy, and extract basic information such as folder names, message subjects, and sender names. It includes basic error handling for file not found and `libpff` specific errors. [8, 19, 12]

import pypff
import os

def process_pst_file(pst_file_path):
    if not os.path.exists(pst_file_path):
        print(f"Error: PST file not found at {pst_file_path}")
        return

    try:
        pst = pypff.file()
        pst.open(pst_file_path)
        print(f"Opened PST file: {pst_file_path}")

        root_folder = pst.get_root_folder()
        print(f"Root folder: {root_folder.get_name()}")

        def recurse_folders(folder, level=0):
            indent = "  " * level
            for sub_folder_index in range(folder.get_number_of_sub_folders()):
                sub_folder = folder.get_sub_folder(sub_folder_index)
                print(f"{indent}- Folder: {sub_folder.get_name()}")

                for message_index in range(sub_folder.get_number_of_sub_messages()):
                    message = sub_folder.get_sub_message(message_index)
                    print(f"{indent}  - Message: {message.get_subject()} (from: {message.get_sender_name()})")
                    # Access other message properties, e.g., message.get_body(), message.get_client_submit_time()

                if sub_folder.get_number_of_sub_folders() > 0:
                    recurse_folders(sub_folder, level + 1)

        recurse_folders(root_folder)
        pst.close()

    except pypff.libpff.error as e:
        print(f"Error processing PST file: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

# Example usage:
# Create a dummy PST file path for demonstration. Replace with your actual PST file.
dummy_pst_path = os.environ.get('PST_FILE_PATH', 'example.pst')
process_pst_file(dummy_pst_path)

view raw JSON →