export Outlook emails to PST with Python win32com.client

187 Views Asked by At
from pathlib import Path
import win32com.client

# Creation du repertoire
output_dir = Path.cwd() / "Backup"
output_dir.mkdir(parents=True, exist_ok=True)

# Acceder à outlook
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

# Recuperer la boite de reception
inbox = outlook.GetDefaultFolder(6)

# lien du chemin
pst = str(output_dir / "backup.pst")

# Export inbox folder to PST file
inbox.Export(pst)

I'm trying to export my inbox from Outlook as a pst file but I'm getting an error with the export method. Does anyone have an idea?

1

There are 1 best solutions below

0
Dmitry Streblechenko On

Call Namespace.AddStoreEx (where Namespace is your outlook variable), find the new Store object in the Namespace.Stores collection, then copy the Inbox folder to the new store (inbox.CopyTo(Store.GetRootFolder()))