用python解析outlook .msg文件

时间:2014-10-12 05:41:27

标签: python email outlook

环顾四周,无法找到满意的答案。有谁知道如何使用Python解析outlook中的.msg文件?

我尝试过使用mimetools和email.parser但没有运气。非常感谢帮助!

7 个答案:

答案 0 :(得分:30)

这对我有用:

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

答案 1 :(得分:7)

我成功地使用Matt Walker的msg-extractor实用程序从MS Outlook文件(.msg)中提取相关字段。

Prerequesites

pip install extract-msg

注意,可能需要安装其他模块,在我的情况下,需要安装imapclient:

pip install imapclient

用法

import extract_msg

f = r'MS_Outlook_file.msg'  # Replace with yours
msg = extract_msg.Message(f)
msg_sender = msg.sender
msg_date = msg.date
msg_subj = msg.subject
msg_message = msg.body

print('Sender: {}'.format(msg_sender))
print('Sent On: {}'.format(msg_date))
print('Subject: {}'.format(msg_subj))
print('Body: {}'.format(msg_message))

在MsgExtractor实用程序中还有许多其他好东西需要探索,但这一点很好。

注意

我必须在文件C:\ Anaconda3 \ Scripts \ ExtractMsg.py中注释第3行到第8行:

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

错误消息是:

line 3
    ExtractMsg:
              ^
SyntaxError: invalid syntax

阻塞这些行后,错误消息消失,代码工作正常。

答案 2 :(得分:3)

即使这是一个旧帖子,我希望这些信息可以帮助那些正在寻找解决方案完全所说的主题的人。我强烈建议您使用mattgwwalker in github的解决方案,该解决方案需要在外部安装OleFileIO_PL module

答案 3 :(得分:1)

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

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

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

答案 4 :(得分:1)

我在网上发现了一个名为MSG PY的模块。 这是用于Python的Microsoft Outlook .msg文件模块。 该模块使您可以轻松创建/读取/解析/转换Outlook .msg文件。 该模块不需要在计算机或任何其他第三方应用程序或库上安装Microsoft Outlook即可正常工作。 例如:

from independentsoft.msg import Message

appointment = Message("e:\\appointment.msg")

print("subject: " + str(appointment.subject))
print("start_time: " + str(appointment.appointment_start_time))
print("end_time: " + str(appointment.appointment_end_time))
print("location: " + str(appointment.location))
print("is_reminder_set: " + str(appointment.is_reminder_set))
print("sender_name: " + str(appointment.sender_name))
print("sender_email_address: " + str(appointment.sender_email_address))
print("display_to: " + str(appointment.display_to))
print("display_cc: " + str(appointment.display_cc))
print("body: " + str(appointment.body))

答案 5 :(得分:0)

我尝试过python电子邮件模块,有时候无法成功解析msg文件。

所以,在这种情况下,如果你只是在text或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)

答案 6 :(得分:0)

我能够像弗拉基米尔上面提到的那样解析它。但是我需要通过添加for循环进行小的更改。 glob.glob(r'c:\ test_email * .msg')返回一个列表,而Message(f)则返回一个文件或str。

f = glob.glob(r'c:\test_email\*.msg')

for filename in f:
    msg = ExtractMsg.Message(filename)
    msg_sender = msg.sender
    msg_date = msg.date
    msg_subj = msg.subject
    msg_message = msg.body
相关问题