Created
September 13, 2020 13:42
-
-
Save Steven1677/865e30ec20a4f4eb119cd75bb3c570a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import re | |
import win32com.client as client | |
def get_gal(): | |
outlook = client.Dispatch('Outlook.Application') | |
entries = outlook.Session.GetGlobalAddressList().AddressEntries | |
gal = [] | |
for entry in entries: | |
try: | |
name = entry.GetExchangeUser().Name | |
match_res = re.match(r'(.*) \((\d{8})\)', name) | |
code = match_res.group(2) | |
name = match_res.group(1) | |
email = entry.GetExchangeUser().PrimarySMTPAddress | |
except AttributeError: | |
continue | |
gal.append({'name': name, 'id': code, 'email': email}) | |
return gal | |
def write_gal_csv(gal): | |
field_names = [*gal[0]] | |
with open('gal.csv', 'w', newline='') as csvfile: | |
writer = csv.DictWriter(csvfile, fieldnames=field_names) | |
writer.writeheader() | |
writer.writerows(gal) | |
def main(): | |
gal = get_gal() | |
write_gal_csv(gal) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment