import win32com.client # Outlook stuff outApp = win32com.client.gencache.EnsureDispatch("Outlook.Application") outGAL = outApp.Session.GetGlobalAddressList() entries = outGAL.AddressEntries # entryFilter = entries.Filter # entryFilter.Fields['PR_EMAIL_ADDRESS'] = "Lydia.LiuHR@hh.cn" # cio_entrie = [ i for i in entries if i.entry.Type == 'EX' and i.GetExchangeUser().PrimarySmtpAddress == 'Eddy.Tai@hh.cn' ] # root_mail = "Basker.Rangachari@hh.cn" # # root_mail = "Eddy.Tai@hh.cn" # filetag = "market" # root_mail = "Cindy.WangTJ@hh.cn" # root_mail = "Lydia.LiuHR@hh.cn" root_list = { "aa.yan@hh.cn": "security", "bb.Nyc@hh.cn": "legal" } def get_root_entries(root_list): root_entries = dict() lencnt = len(root_list) mail_list = root_list.keys() print(mail_list) for i in entries: if i.Type == 'EX': user = i.GetExchangeUser() if user is not None: addr = user.PrimarySmtpAddress if addr is not None and addr in mail_list: root_entries[root_list[addr]] = i print(addr) if len(root_entries) == lencnt: break return root_entries def gogo(filetag, root_entry): def as_list(x): if type(x) is list: return x else: return [x] dig_flag = True # Empty list to store contact info data_set = list() def prt_entrys(v_sub_entrys, nlevel): sub_member_count = 0 if v_sub_entrys is None: return 0 for entry in v_sub_entrys: if entry.Type == "EX": user = entry.GetExchangeUser() if user is not None and len(user.FirstName) > 0 and len(user.LastName) > 0: # row = list() # row.append(user.FirstName) # row.append(user.LastName) # row.append(user.PrimarySmtpAddress) # print("First Name: " + user.FirstName) # print("Last Name: " + user.LastName) ret_sub_member_count = 0 if dig_flag: sub_entrys = user.GetDirectReports() ret_sub_member_count = prt_entrys(sub_entrys, nlevel + 1) if ret_sub_member_count > 0: sub_member_count += ret_sub_member_count + 1 str1 = "\t" * nlevel + f"{ret_sub_member_count + 1} {user.PrimarySmtpAddress} {user.Department} {user.MobileTelephoneNumber} {user.JobTitle} " else: sub_member_count += 1 str1 = "\t" * nlevel + f"{user.PrimarySmtpAddress} {user.Department} {user.MobileTelephoneNumber} {user.JobTitle} " print(str1) data_set.append(str1) return sub_member_count # Iterates through your contact book and extracts/appends them to a list for entry in as_list(root_entry): if entry.Type == "EX": user = entry.GetExchangeUser() if user is not None and len(user.FirstName) > 0 and len(user.LastName) > 0: # row = list() # row.append(user.FirstName) # row.append(user.LastName) # row.append(user.PrimarySmtpAddress) # print(f"{user.PrimarySmtpAddress} {user.Department} {user.MobileTelephoneNumber} {user.JobTitle}") # data_set.append(row) sub_entrys = user.GetDirectReports() sub_member_count = prt_entrys(sub_entrys, 1) str1 = f"{sub_member_count} {filetag} {user.PrimarySmtpAddress} {user.Department} {user.MobileTelephoneNumber} {user.JobTitle} " print(str1) data_set.append(str1) # Prints list # print(data_set) print('len of data_set:', len(data_set)) data_set.reverse() with open(f"all.txt", "a+", encoding="utf-8") as inFile: # for l in data_set: # print(l) inFile.writelines("\n".join(data_set)) inFile.write("\n") inFile.close() if __name__ == '__main__': open(f"all.txt", 'w').close() root_entries = get_root_entries(root_list) for filetag, root_entry in root_entries.items(): print(filetag) gogo(filetag, root_entry)