I've been utilizing pywin32 in Python to extract names, email IDs, job titles, and other details of all members from specified Outlook distribution lists.
Initially, the code executed very quickly; although I lack an exact estimate, it could extract all pertinent details for even 100-200 or more members within 1-2 minutes.
However, after changing laptops within my organization, I've noticed a significant slowdown. The code now takes up to an hour for similar outputs. Despite searching online, I haven't found any relevant mentions of such an issue. Your guidance would be appreciated.
A sample of the code:
outApp = win32com.client.gencache.EnsureDispatch('Outlook.Application').GetNamespace("MAPI")
entries = outApp.AddressLists
dist_lists = entries['All Distribution Lists']
outlookdf=pd.DataFrame(columns=['User Name','Email','Job Level','Location','Department','DL Name'])
dl_names_list = ["<insert name of distribution list here>"]
print("Distribution Lists being extracted are: ")
for i in dl_names_list:
print(str(i))
print("----------------------------")
#########GET DL Names from list and put it in a dataframe
for i in dl_names_list:
print("Distribution List now being extracted: ")
print(str(i))
#initialize widgets
widgets = ['Remaining Time: ', pb.Percentage(), ' ',
pb.Bar(marker=pb.RotatingMarker()), ' ', pb.ETA()]
#initialize timer
timer = pb.ProgressBar(widgets=widgets, maxval=len(dist_lists.AddressEntries.Item(str(i)).GetExchangeDistributionList().Members)).start()
ind = 0
m_list_1 = []
m_list_2 = []
m_list_3 = []
m_list_4 = []
m_list_5 = []
for m in dist_lists.AddressEntries.Item(str(i)).GetExchangeDistributionList().Members:
user=m.GetExchangeUser()
try:
if len(user.Name) > 0 and (user.Name.find(', ') != -1):
value1 = user.Name
m_list_1.append(value1)
value2 = user.PrimarySmtpAddress
m_list_2.append(value2)
value3 = user.JobTitle
m_list_3.append(value3)
value4 = user.OfficeLocation
m_list_4.append(value4)
value5 = user.Department
m_list_5.append(value5)
timer.update(ind)
ind = ind + 1
except:
continue
timer.finish()
print("---------------------------------------------------")
df_m = pd.DataFrame(pd.DataFrame(
{'User Name': m_list_1,
'Email': m_list_2,
'Job Level': m_list_3,
'Location': m_list_4,
'Department': m_list_5
}))
df_m["DL Name"] = str(dist_lists.AddressEntries.Item(str(i)).GetExchangeDistributionList().Name)
outlookdf = pd.concat([outlookdf,df_m])
outlookdf = outlookdf.drop_duplicates()
outlookdf = outlookdf.reset_index(drop = True)
I found the solution; which was to simply download the Address Book in Outlook so that it would use the Offline Address Book (updated once every 48 hours). Previously Python was connecting to Address Book (Global Address List) in external server which is why the code was taking such a long time. Hope this is helpful to anyone facing similar issue in future.