[python-win32] Obtaining Outlook item properties from saved email file

Tim Golden mail at timgolden.me.uk
Mon Feb 16 16:03:40 CET 2009


Paul Moore wrote:
> Sorry, this is only peripherally a Python issue, as the code I'm
> writing could just as easily be VBScript or any other language... But
> as I'm writing in Python, and this group is full of helpful and
> knowledgeable people... :-)
> 
> I have a folder full of emails saved from Outlook (.msg extension). I
> want to extract from those files, the subject, sent/received date,
> author, etc. Getting the properties isn't hard once I have a CDO
> message object (or equivalent) to work with, but I've been unable to
> find any way of getting a message object when all I'm given is the
> filename of a ".msg" file in the filesystem.
> 
> Can anybody suggest a way to do this, or point me at sample code?

They're structured storage and a bit opaque. In principle
this (very sketchy and untested) code should get you started:

<code>
import pythoncom
from win32com import storagecon

filename = r"c:\temp\outlook.msg"
print pythoncom.StgIsStorageFile (filename)

flags = storagecon.STGM_READ | storagecon.STGM_SHARE_EXCLUSIVE
storage = pythoncom.StgOpenStorage (filename, None, flags)

for data in storage.EnumElements ():
  if data[1] == 2:
    print data[0]
    stream = storage.OpenStream (data[0], None, flags)
    print repr (stream.Read (data[2]))

#
# ... and now you've got an IStream interface which
# you can read etc.
#

</code>

There are some stream names listed here:

http://www.msusenet.com/archive/topic.php/t-288764.html 

TJG


More information about the python-win32 mailing list