Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98217 > unrolled thread

Re: Creating PST files using Python

Started byTim Golden <mail@timgolden.me.uk>
First post2015-11-04 10:08 +0000
Last post2015-11-04 10:08 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Creating PST files using Python Tim Golden <mail@timgolden.me.uk> - 2015-11-04 10:08 +0000

#98217 — Re: Creating PST files using Python

FromTim Golden <mail@timgolden.me.uk>
Date2015-11-04 10:08 +0000
SubjectRe: Creating PST files using Python
Message-ID<mailman.9.1446631726.16136.python-list@python.org>
On 03/11/2015 22:12, Chris Angelico wrote:
> On Wed, Nov 4, 2015 at 6:09 AM, Anthony Papillion 
> <anthony@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)

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web