Convert MBOX thunderbird to PST outlook

Kr4ck ALI kr4ckali at gmail.com
Sun Feb 7 09:06:17 EST 2021


Hello,

I have to migrate multiple mailbox (emails, contacts, calendar, tasks) from
thunderbird to outlook Office 365.
I plan to export all items from thunderbird files (.mbox for email, .sqlite
or .sdb for calendar, .mab to contact) to PST files and import each PST
files to Office 365.
I know it's a tedious task ... But I know that nothing is impossible !
I found this script very helpfull to begin :

#!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)

My question is : Have you already make a mission like that ?

Thanks in advance !

@Kr4ckali


More information about the Python-list mailing list