Skip to content

Python development

Joachim Metz edited this page Jul 13, 2022 · 4 revisions

libpff comes with Python-bindings named pypff.

Below are examples how use pypff. They assume you have a working version of pypff on your system. To build pypff see Building.

Import

To be able to use pypff in your Python scripts add the following import:

import pypff

Get version

The get_version() module function can be used to retrieve the version of the pypff.

pypff.get_version()

This will return a textual string (Unicode) that contains the libpff version. Since pypff is a wrapper around libpff it does not have a separate version.

Open file

pff_file = pypff.file()

pff_file.open("Archive.pst")

pff_file.close()

The explicit call to pff_file.close() is not required. Close only must be called once all operations on the file have been completed.

Open file using a file-like object

file_object = open("Archive.pst", "rb")

pff_file = pypff.file()

pff_file.open_file_object(file_object)

pff_file.close()

The explicit call to pff_file.close() is not required. Close only must be called once all operations on the file have been completed.

Items

A pypff.item is the base object for the different kind of items stored in the PFF file.

Get the root folder

The number of items can be retrieved by either calling the get_number_of_items() function, e.g.

pff_root_folder = pff_file.get_root_folder()

or reading the root_folder property:

pff_root_folder = pff_file.root_folder

Get number of items

The number of items can be retrieved by either calling the get_number_of_items() function, e.g.

number_of_items = pff_root_folder.get_number_of_items()

or reading the number_of_items property:

number_of_items = pff_root_folder.number_of_items

The number of orphaned items can be obtained by calling the function pff_file.get_number_of_orphan_items() or reading the property pff_file.number_of_orphan_items.

Get an item by index

An item can be retrieved by index:

pff_item = pff_root_folder.get_item(0)

The function will return the most specific item object type. This will either be pypff.item, pypff.folder, pypff.message. Where pypff.item is the base type for the other types.

An orphaned item can be retrieved in the same way by using the function pff_file.get_orphan_item().

Iterating over all items in the root folder

pff_root_folder = pff_file.get_root_folder()
for pff_item in pff_root_folder.items:
    if isinstance(pff_item, pypff.url):
        print(pff_item.location)

The recovered items can iterated by reading the property pff_file.orphan_items.

Also see

import pypff

help(pypff)
help(pypff.file)
help(pypff.item)
help(pypff.folder)
help(pypff.message)