Parse Outlook PST Files in Python

Parse Outlook PST Files in Python

PST (Personal Storage Table) is a storage file format that is used by different Microsoft programs. PST files are capable of keeping data of messages, contacts, calendars, events, etc. It is used by popular Microsoft softwares such as MS Outlook, Exchange, and Windows Messaging. In certain cases, you may need to parse a PST file and extract data from it programmatically. To achieve that, this article shows how to parse PST files from within the Python applications.

Python Library to Parse PST Files

In order to parse PST files, we will use Aspose.Email for Python. It is a powerful Python library that lets you implement feature-rich email clients seamlessly. Furthermore, it allows you to work with popular email and storage formats. You can install the library from PyPI using the following command.

> pip install Aspose.Email-for-Python-via-NET

Parse a PST File in Python

The following are the steps to parse a PST file and extract its information in Python.

  • Load the PST file using PersonalStorage.from_file() method.
  • Get the folders collection in PST using PersonalStorage.root_folder.get_sub_folders() method.
  • Retrieve the information of the folders such as name, number of items, etc.

The following code sample shows how to parse a PST file and fetch folders’ information.

Extract Messages from a PST File in Python

Messages are the major entities that are stored in PST files by Microsoft programs such as MS Outlook. So let’s see how to extract messages from a PST file in Python.

  • Load the PST file using PersonalStorage.from_file() method.
  • Get the reference of the desired folder in an object using PersonalStorage.root_folder.get_sub_folder() method.
  • Get message collection using get_contents() method.
  • Loop through the messages in the collection and read each message’s fields.

The following code sample shows how to extract messages from a PST file in Python.

Extract Contacts from a PST File in Python

In certain cases, you may need to extract the contacts’ information stored in a PST file. The following steps show how to access the contacts in a PST file in Python.

  • Load the PST file using PersonalStorage.from_file() method.
  • Get reference of the contacts folder using get_predefined_folder(StandardIpmFolder.CONTACTS) method.
  • Get collection of contacts using get_contents() method.
  • Loop through the collection and read each contact or save it.

The following code sample shows how to extract contacts from a PST file in Python.

Get a Free API License

You can try Aspose.Email for Python for free by getting a temporary license.

Conclusion

In this article, you have learned how to parse PST files in Python. Furthermore, you have seen how to extract contacts and messages from a PST file programmatically in Python. In addition, you can explore more about Aspose.Email using documentation. Also, you can share your questions or queries on our forum.

See Also