You may use the following code.
import win32com.client
import pandas as pd
# Outlook stuff
outApp = win32com.client.gencache.EnsureDispatch("Outlook.Application")
outGAL = outApp.Session.GetGlobalAddressList()
entries = outGAL.AddressEntries
data_set = list()
# Iterates through your contact book and extracts/appends them to a list
for entry in entries:
if entry.Type == "EX":
user = entry.GetExchangeUser()
if user is not None:
if len(user.FirstName) > 0 or len(user.LastName) > 0:
row = list()
row.append(user.FirstName)
row.append(user.LastName)
row.append(user.PrimarySmtpAddress)
row.append(user.Department)
row.append(user.BusinessTelephoneNumber)
row.append(user.MobileTelephoneNumber)
row.append(user.CompanyName)
row.append(user.Name)
row.append(user.JobTitle)
row.append(user.OfficeLocation)
row.append(user.Alias)
row.append(user.City)
row.append(user.Comments)
row.append(user.StateOrProvince)
row.append(user.StreetAddress)
data_set.append(row)
first,second,email,dept,office,mobile,company,outlook_name,title,location,alias,city,state,postal = [],[],[],[],[],[],[],[],[],[],[],[],[],[]
for ea in data_set:
first.append(ea[0])
second.append(ea[1])
email.append(ea[2])
dept.append(ea[3])
office.append(ea[4])
mobile.append(ea[5])
company.append(ea[6])
outlook_name.append(ea[7])
title.append(ea[8])
location.append(ea[9])
alias.append(ea[10])
city.append(ea[11])
state.append(ea[13])
postal.append(ea[14])
address = pd.DataFrame()
address['Full Name']= outlook_name
address['email']=email
address['Department'] = dept
address['Office No.']=office
address['Mobile No.'] = mobile
address['Company']=company
address['City'] = city
address['Title']= title
address['Office']= location
address['Alias'] = alias
address['First Name']=first
address['Last Name']=second
address['Province'] =state
address['Postal Address'] = postal
address.to_excel('Address_book with '+str(address.shape[0])+' contacts.xlsx',index=None,freeze_panes =(1,0))