Parsing outlook .msg files with python Parsing outlook .msg files with python python python

Parsing outlook .msg files with python


This works for me:

import win32com.clientoutlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")msg = outlook.OpenSharedItem(r"C:\test_msg.msg")print msg.SenderNameprint msg.SenderEmailAddressprint msg.SentOnprint msg.Toprint msg.CCprint msg.BCCprint msg.Subjectprint msg.Bodycount_attachments = msg.Attachments.Countif count_attachments > 0:    for item in range(count_attachments):        print msg.Attachments.Item(item + 1).Filenamedel outlook, msg

Please refer to the following post regarding methods to access email addresses and not just the names (ex. "John Doe") from the To, CC and BCC properties - enter link description here


I succeeded extracting relevant fields from MS Outlook files (.msg) using msg-extractor utilitity by Matt Walker.

Prerequesites

pip install extract-msg

Note, it may require to install additional modules, in my case, it required to install imapclient:

pip install imapclient

Usage

import extract_msgf = r'MS_Outlook_file.msg'  # Replace with yoursmsg = extract_msg.Message(f)msg_sender = msg.sendermsg_date = msg.datemsg_subj = msg.subjectmsg_message = msg.bodyprint('Sender: {}'.format(msg_sender))print('Sent On: {}'.format(msg_date))print('Subject: {}'.format(msg_subj))print('Body: {}'.format(msg_message))

There are many other goodies in MsgExtractor utility, to be explored, but this is good to start with.

Note

I had to comment out lines 3 to 8 within the file C:\Anaconda3\Scripts\ExtractMsg.py:

#"""#ExtractMsg:#    Extracts emails and attachments saved in Microsoft Outlook's .msg files##https://github.com/mattgwwalker/msg-extractor#"""

Error message was:

line 3    ExtractMsg:              ^SyntaxError: invalid syntax

After blocking those lines, the error message disappeared and the code worked just fine.


Even though this is an old thread, I hope this information might help someone who is looking for a solution to what the thread subject exactly says. I strongly advise using the solution of mattgwwalker in github, which requires OleFileIO_PL module to be installed externally.