2014-10-12 17 views
12

Si guardò intorno e non riuscì a trovare una risposta soddisfacente. Qualcuno sa come analizzare i file .msg da Outlook con Python?Analisi dei file .msg di Outlook con Python

Ho provato a usare mimetools e email.parser senza fortuna. L'aiuto sarebbe molto apprezzato!

risposta

12

Questo funziona per me:

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

È importante notare che il metodo OpenSharedItem prevede un percorso assoluto altrimenti si ottiene un errore. – smartexpert

+1

Mi sembra di avere problemi con la codifica. Come puoi risolvere quello? – firko

3

Anche se questo è un vecchio filo, Spero che questa informazione potrebbe aiutare qualcuno che è alla ricerca di una soluzione a quello che il filo soggetto esattamente dice. Consiglio vivamente di utilizzare la soluzione di mattgwwalker in github, che richiede l'installazione esterna di OleFileIO_PL module.

0

Ho provato il modulo python-mail e, talvolta, che non analizza correttamente il file msg.

Quindi, in questo caso, se si utilizza solo testo o html, il codice seguente ha funzionato per me.

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)