Skip to content

Instantly share code, notes, and snippets.

@saurabhwahile
Created December 23, 2017 05:47
Show Gist options
  • Save saurabhwahile/c48226701e104f32f92b4c33bf7450de to your computer and use it in GitHub Desktop.
Save saurabhwahile/c48226701e104f32f92b4c33bf7450de to your computer and use it in GitHub Desktop.
Google Contacts import from Excel
import pandas as pd
from openpyxl import load_workbook
index = ['Name', 'Given Name', 'Additional Name', 'Family Name', 'Yomi Name', 'Given Name Yomi', 'Additional Name Yomi', 'Family Name Yomi', 'Name Prefix', 'Name Suffix', 'Initials', 'Nickname', 'Short Name', 'Maiden Name', 'Birthday', 'Gender', 'Location', 'Billing Information', 'Directory Server', 'Mileage', 'Occupation', 'Hobby', 'Sensitivity', 'Priority', 'Subject', 'Notes', 'Group Membership', 'E-mail 1 - Type', 'E-mail 1 - Value', 'E-mail 2 - Type', 'E-mail 2 - Value', 'Phone 1 - Type', 'Phone 1 - Value', 'Phone 2 - Type', 'Phone 2 - Value', 'Phone 3 - Type', 'Phone 3 - Value', 'Phone 4 - Type', 'Phone 4 - Value', 'Phone 5 - Type', 'Phone 5 - Value', 'Phone 6 - Type', 'Phone 6 - Value', 'Phone 7 - Type', 'Phone 7 - Value', 'Address 1 - Type', 'Address 1 - Formatted', 'Address 1 - Street', 'Address 1 - City', 'Address 1 - PO Box', 'Address 1 - Region', 'Address 1 - Postal Code', 'Address 1 - Country', 'Address 1 - Extended Address', 'Organization 1 - Type', 'Organization 1 - Name', 'Organization 1 - Yomi Name', 'Organization 1 - Title', 'Organization 1 - Department', 'Organization 1 - Symbol', 'Organization 1 - Location', 'Organization 1 - Job Description', 'Website 1 - Type', 'Website 1 - Value']
def create_contact(cdict):
contact = {}
for field in index:
contact[field] = ''
for key, value in cdict.items():
try:
contact[key]
contact[key] = value
except KeyError:
print('Error: No key '+key)
return contact
wb = load_workbook(filename = 'filename.xlsx')
ws = wb.active
contacts_list = []
for row in ws.iter_rows(row_offset=1):
contact = create_contact({
'Name':
(row[3].value+' ' if row[3].value else '')+
(row[4].value+' ' if row[4].value else '')+
(row[2].value+' ' if row[2].value else '')+
'SURGEON',
'Phone 1 - Type': 'Mobile',
'Phone 1 - Value': row[6].value if row[6].value else '',
'E-mail 1 - Type': '* ',
'E-mail 1 - Value': row[7].value if row[7].value else '',
'E-mail 2 - Type': '* Other',
'E-mail 2 - Value': row[8].value if row[8].value else ''
})
contacts_list.append(contact)
contacts = pd.DataFrame(contacts_list)
contacts.to_csv('exported.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment