Skip to content

Instantly share code, notes, and snippets.

@Steven1677
Created September 13, 2020 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Steven1677/865e30ec20a4f4eb119cd75bb3c570a9 to your computer and use it in GitHub Desktop.
Save Steven1677/865e30ec20a4f4eb119cd75bb3c570a9 to your computer and use it in GitHub Desktop.
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