Hi Experts.
Below is the python code which I’m using for reading mails from a mailbox.
I’m reading mails from Inbox and Subfolder. In this I had around 25 thousands of mails, for this it is taking 16 minutes.
My question is: is there any efficient way in python code to get the results within short time?
If is there any answers that will very helpful and appreciated.
import win32com.client as client
import pandas as pd
import time
start_time = time.time()
#messages.Sort("[ReceivedTime]", True)
outlook = client.Dispatch("Outlook.Application").GetNamespace("MAPI")
# XXXXXXXXXXXXXX == mentioning mail ID here
#Accessing mail box
root_folder = outlook.Folders.Item('XXXXXX')
#inbox messges
print("reading Inbox...")
Inbox = root_folder.Folders['Inbox']
inbox_messages = Inbox.items
inbox_messages.Sort("[ReceivedTime]", True)
#Accessing particular folder 'Redeployment' in Inbox
print("reading Inbox Sub folder...")
#accessing subfolder inside the Inbox
Inbox_subfolder = Inbox.Folders['xxxxxxxxxx']
subfolder_messages = Inbox_subfolder.items
subfolder_messages.Sort("[ReceivedTime]", True)
#list to store inbox mails in dataframe
inbox_list = []
#list to store subfolder mails in dataframe
subfolder_list = []
#iterating inbox messages
print("Retrieving mails from Inbox")
for message in inbox_messages:
print(f"message count: {len(message)}")
if message.Class == 43:
received_date = message.ReceivedTime.strftime('%d-%m-%Y %I:%M:%S')
inbox_list.append((message.Subject,message.Sender,message.To,received_date,message.body,message,Inbox))
#Inbox messages to dataframe
inbox_df = pd.DataFrame(inbox_list)
inbox_df.columns =['Subject','Sender', 'Receipients','Received Date','Mail Body','Message','Folder']
mid_time = time.time()
print("Reading Inbox mails time taken is {}\n\n".format(mid_time-start_time))
#iterating inbox messages
print("\nRetrieving mails from subfolder\n")
for message in subfolder_messages:
if message.Class == 43:
received_date = message.ReceivedTime.strftime('%d-%m-%Y %I:%M:%S')
subfolder_list.append((message.Subject, message.Sender,message.To,received_date,message.body,message,Inbox_subfolder))
#subfolder messages to dataframe
subfolder_df = pd.DataFrame(subfolder_list)
subfolder_df.columns =['Subject','Sender', 'Receipients','Received Date','Mail Body','Message','Folder']
# using a .concat() method
frames = [inbox_df, subfolder_df]
final_mail_df = pd.concat(frames)
end_time = time.time()
print("\nReading subfolder mails time taken is {}\n\n".format(end_time-start_time))
#print(final_mail_df)
final_mail_df.to_excel("mail_dataframe.xlsx",index=False)
print("\nCompleted")