2017-08-15 2 views
0

J'ai un script Python qui utilise os.walk et win32com.client pour extraire des informations des fichiers de messagerie Outlook (.msg) d'un dossier et de ses sous-dossiers sur mon lecteur C: /. Il semble fonctionner, mais quand j'essaye de faire n'importe quoi sur le dataframe retourné (tel que emailData.head() accidents de Python). Je ne peux pas écrire l'image de données à .csv en raison d'une erreur d'autorisation.Erreur lors de l'extraction des données de messagerie Outlook avec Python

Je me demande si mon code ne ferme pas correctement Outlook/chaque message et qu'est-ce qui cause le problème? Toute aide serait appréciée.

import os 
import win32com.client 
import pandas as pd 

# initialize Outlook client 
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 

# set input directory (where the emails are) and output directory (where you 
# would like the email data saved) 
inputDir = 'C:/Users/.../myFolderPath' 
outputDir = 'C:/Users/.../myOutputPath' 


def emailDataCollection(inputDir,outputDir): 
    """ This function loops through an input directory to find 
    all '.msg' email files in all folders and subfolders in the 
    directory, extracting information from the email into lists, 
    then converting the lists to a Pandas dataframe before exporting 
    to a '.csv' file in the output directory 
    """ 
    # Initialize lists 
    msg_Path = [] 
    msg_SenderName = [] 
    msg_SenderEmailAddress = [] 
    msg_SentOn = [] 
    msg_To = [] 
    msg_CC = [] 
    msg_BCC = [] 
    msg_Subject = [] 
    msg_Body = [] 
    msg_AttachmentCount = [] 

    # Loop through the directory 
    for root, dirnames, filenames in os.walk(inputDir): 
     for filename in filenames: 
      if filename.endswith('.msg'): # check to see if the file is an email 
       filepath = os.path.join(root,filename) # save the full filepath 
       # Extract email data into lists 
       msg = outlook.OpenSharedItem(filepath) 
       msg_Path.append(filepath) 
       msg_SenderName.append(msg.SenderName) 
       msg_SenderEmailAddress.append(msg.SenderEmailAddress) 
       msg_SentOn.append(msg.SentOn) 
       msg_To.append(msg.To) 
       msg_CC.append(msg.CC) 
       msg_BCC.append(msg.BCC) 
       msg_Subject.append(msg.Subject) 
       msg_Body.append(msg.Body) 
       msg_AttachmentCount.append(msg.Attachments.Count) 
       del msg 

    # Convert lists to Pandas dataframe 
    emailData = pd.DataFrame({'Path' : msg_Path, 
          'SenderName' : msg_SenderName, 
          'SenderEmailAddress' : msg_SenderEmailAddress, 
          'SentOn' : msg_SentOn, 
          'To' : msg_To, 
          'CC' : msg_CC, 
          'BCC' : msg_BCC, 
          'Subject' : msg_Subject, 
          'Body' : msg_Body, 
          'AttachmentCount' : msg_AttachmentCount 
    }, columns=['Path','SenderName','SenderEmailAddress','SentOn','To','CC', 
      'BCC','Subject','Body','AttachmentCount']) 


    return(emailData) 


# Call the function 
emailData = emailDataCollection(inputDir,outputDir) 

# Causes Python to crash 
emailData.head() 
# Fails due to permission error 
emailData.to_csv(outputDir,header=True,index=False) 

Répondre

1

Espérons que cela est pas trop tard, mais je réussi à trouver la source du problème:

Le noyau est écrasé à cause des données datetime de msg_SentOn. Si vous vérifiez le type() des données dans msg_SentOn, il est classé comme pywintype.datetime, ce qui est incompatible avec les pandas.

Vous devez convertir les éléments au format msg_SentOn au format datetime.datetime.

La source ici est utile de le faire: http://timgolden.me.uk/python/win32_how_do_i/use-a-pytime-value.html