使用python解析outlook .msg文件

使用python解析outlook .msg文件

问题描述:

四处寻找,找不到满意的答案。有谁知道如何从Python与Outlook解析.msg文件?使用python解析outlook .msg文件

我试过用mimetools和email.parser没有运气。帮助将不胜感激!

这个工作对我来说:

import win32com.client 
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 
msg = outlook.OpenSharedItem(r"C:\test_msg.msg") 

print msg.SenderName 
print msg.SenderEmailAddress 
print msg.SentOn 
print msg.To 
print msg.CC 
print msg.BCC 
print msg.Subject 
print msg.Body 

count_attachments = msg.Attachments.Count 
if count_attachments > 0: 
    for item in range(count_attachments): 
     print msg.Attachments.Item(item + 1).Filename 

del outlook, msg 
+4

重要的是要注意OpenSharedItem方法需要一个绝对路径,否则你会得到一个错误。 – smartexpert 2016-06-17 08:58:01

+1

我似乎有编码问题。你怎么解决这个问题? – firko 2017-03-01 20:39:56

请访问以下链接:
[MS-OXMSG]: Outlook Item (.msg) File Format
Read from .msg files
Edit a saved Outlook Message File *.msg

您还可以使用Redemption及其RDOSessionGetMessageFromMsgFile方法:

set Session = CreateObject("Redemption.RDOSession") 
    set Msg = Session.GetMessageFromMsgFile("c:\temp\test.msg") 
    MsgBox Msg.Subject 

即使这是一个古老的线程,我希望这个信息可以帮助的人谁是寻找一个解决方案,以什么的线程主题正是说。我强烈建议使用mattgwwalker in github的解决方案,这需要在外部安装OleFileIO_PL module

我已经试过了蟒蛇电子邮件模块,有时并不成功解析味精文件。

所以,在这种情况下,如果你只是在文本或HTML之后,下面的代码为我工作。

start_text = "<html>" 
end_text = "</html>" 
def parse_msg(msg_file,start_text,end_text): 
    with open(msg_file) as f: 
    b=f.read() 
    return b[b.find(start_text):b.find(end_text)+len(end_text)] 

print parse_msg(path_to_msg_file,start_text,end_text)