Creating PST files using Python

Tim Golden mail at timgolden.me.uk
Wed Nov 4 05:08:41 EST 2015


On 03/11/2015 22:12, Chris Angelico wrote:
> On Wed, Nov 4, 2015 at 6:09 AM, Anthony Papillion 
> <anthony at cajuntechie.org> wrote:
>> Does anyone know of a module that allows the wiring of Outlook PST
>> files using Python? I'm working on a project that will require me
>> to migrate 60gb of maildir mail (multiple accounts) to Outlook.

I don't know if this is quite what you were after but, if automating
Outlook is an option (ie using its COM object model), the code below is
an *extremely* Q&D approach to adding a PST and copying the messages in
a mbox one by one.
Obviously I've done just enough to show that it's viable, and none of
the more complicated work around conversation ids, multipart messages etc.
TJG
#!python3
import os, sys
import gzip
import mailbox
import urllib.request
import win32com.client
dispatch = win32com.client.gencache.EnsureDispatch
const = win32com.client.constants
PST_FILEPATH =
os.path.abspath(os.path.join(os.path.expandvars("%APPDATA%"),
"scratch.pst"))
if os.path.exists(PST_FILEPATH):
 os.remove(PST_FILEPATH)
ARCHIVE_URL =
"https://mail.python.org/pipermail/python-list/2015-November.txt.gz"
MBOX_FILEPATH = "archive.mbox"
def download_archive(url, local_mbox):
 with gzip.open(urllib.request.urlopen(url)) as archive:
 with open(local_mbox, "wb") as f:
 print("Writing %s to %s" % (url, local_mbox))
 f.write(archive.read())
def copy_archive_to_pst(mbox_filepath, pst_folder):
 archive = mailbox.mbox(mbox_filepath)
 for message in archive:
 print(message.get("Subject"))
 pst_message = pst_folder.Items.Add()
 pst_message.Subject = message.get("Subject")
 pst_message.Sender = message.get("From")
 pst_message.Body = message.get_payload()
 pst_message.Move(pst_folder)
 pst_message.Save()
def find_pst_folder(namespace, pst_filepath):
 for store in dispatch(mapi.Stores):
 if store.IsDataFileStore and store.FilePath == PST_FILEPATH:
 return store.GetRootFolder()
download_archive(ARCHIVE_URL, MBOX_FILEPATH)
outlook = dispatch("Outlook.Application")
mapi = outlook.GetNamespace("MAPI")
pst_folder = find_pst_folder(mapi, PST_FILEPATH)
if not pst_folder:
 mapi.AddStoreEx(PST_FILEPATH, const.olStoreDefault)
 pst_folder = find_pst_folder(mapi, PST_FILEPATH)
if not pst_folder:
 raise RuntimeError("Can't find PST folder at %s" % PST_FILEPATH)
copy_archive_to_pst(MBOX_FILEPATH, pst_folder)


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /