Skip to content

Instantly share code, notes, and snippets.

@dado3212
Last active September 25, 2017 05:21
Show Gist options
  • Save dado3212/2583fd1c2936ee19dbabfbeb8010a137 to your computer and use it in GitHub Desktop.
Save dado3212/2583fd1c2936ee19dbabfbeb8010a137 to your computer and use it in GitHub Desktop.
Creates one giant vCard from a multitude of contacts
# Name: Alex Beals
# Description: Turn an array of contact data into a single vCard for easy import
# Returns a string created from a first and last name, and an optional phone number and email address
def createContact(firstname, lastname, phone="", email=""):
contact = "BEGIN:VCARD\r\n"
contact += "VERSION:2.1\r\n"
contact += "N;LANGUAGE=en-us:" + str(lastname) + ";" + str(firstname) + "\r\n"
contact += "FN:" + str(firstname) + " " + str(lastname) + "\r\n"
if (phone != ""):
contact += "TEL;CELL;VOICE:" + str(phone) + "\r\n"
if (email != ""):
contact += "EMAIL;PREF;INTERNET:" + str(email) + "\r\n"
contact += "REV:20151004T192713Z\r\n"
contact += "END:VCARD\r\n"
return contact
# Formatting
# ([^\s]*)\s*([^\s]*)\s*(.*)\n
# {"firstName":"\1","lastName":"\2","phone":"\3"},
# Edit this to include all of the contacts that you want. Note that phone number and email address are optional
allContacts = [
{
"firstName":"Sample First",
"lastName":"Sample Last",
"phone":"Sample Phone",
"email":"Sample Email"
},
{
"firstName":"First",
"lastName":"Last",
"email":"noPhoneNumber@gmail.com"
}
]
contactText = ""
for contact in allContacts:
contactText += createContact(
contact["firstName"],
contact["lastName"],
contact["phone"] if "phone" in contact else "",
contact["email"] if "email" in contact else ""
)
with open('Combined.vcf', 'wb') as f:
f.write(contactText)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment