Coding Education STEM

vCards to/from CSV/Excel (and more)

vCards. What are they? Here’s a blurb from wikipedia:
vCard, also known as VCF, is a file format standard for electronic business cards. vCards are often attached to e-mail messages but can be exchanged in other ways, such as Multimedia Messaging Service, on the World Wide Web, instant messaging or through QR code. They can contain name and address information, telephone numbers, e-mail addresses, URLs, logos, photographs, and audio clips.

vCards are used for sending contact details (including embedded photos, urls) with others via attaching the file as an email, text message, or just drop in a file share. This is a common format for sharing from most smartphones. Once it’s received, user can open it easily by double-clicking it. They can be opened in Windows Contacts, Outlook, (even Notepad) or import into other systems.

-wikipedia

There are several tools that convert VCF file into a CSV or HTML or PDF files for example. You can also easily import a VCF file into Outlook contact using File>Open & Export>Import/Export. Once a VCF is imported, they can exported again from Outlook in a CSV format that can be opened in Excel. You can use the same idea with Gmail, Live, Yahoo and other email clients that supports import/export as well.

So, let’s look at some scenarios.

  • You have contacts in your cell phones that you need to import into your PC.
  • You want the contacts to reside in a database or spreadsheet for management, analysis, reformatting, marketing, and/or reporting.

To get your contacts from phone to PC or another device as an Excel file or into a database (without using bluetooth or wired connection or 3rd party app), here are the high-level steps: SmartPhone contacts -> Share as Email as VCF. Then in PC, import the VCF into Outlook, and then export it out as CSV. Now you can open it in Excel, Access, SQL, Google Sheets, et al.

Now, let’s turn that around and say you have a database of contacts, but what you want is to share each person’s/business’s contact information via email or file to others without having to share the entire Excel file or database. First, you should export the database or table or list of contacts into a CSV file. Then from that CSV file, you will create individual vCards (VCF files).

VCF to CSV is easier even without needing 3rd party tools as explained above. But to go from CSV to individual VCFs (without using 3rd party tools) you’ll have to write a utility in your favorite language yourself. In this blog, I’ll show you how to write your own app that does just that.

This app converts a CSV file to individual unique VCF files for each contact in the CSV file. It can also alternatively save all the CSV file into a large VCF file, perhaps to import into Outlook contacts in one shot, but with slight modification. Parsing and creating individual VCF files requires a little more preparation…I’ll share that code because once you get that, you can do the easier part to creating a single vCard yourself (i.e. instead of creating and closing each VCF file in the for-loop, we can simply create one all-encompassing VCF file before the loop, keep appending to it, and close it after the loop).

ASSUMPTION: The source CSV file is formatted as these columns:
First Name,Last Name,Display Name,E-mail,Business Phone,Website
with the first row as header.

Basics of a vCard content:
BEGIN:VCARD
VERSION:2.1
….data here
END:VCARD

Data format:
N: (filing name. usually last, first) last;first;;;
FN: (full name)
NICKNAME: (nickname)
TEL;CELL: (mobile #)
TEL;WORK: (business phone#)
EMAIL;WORK: (work email address)
EMAIL;HOME: (personal email address)
EMAIL: (preferred email, or email 1)
ADR;HOME:;;street;city;state;zipcode;country
URL:(web site) --- defaults to personal website
URL;WORK:(business web site)
URL;HOME:(personal web site)

For full definition of the format see here.

Before we create a vCard, let’s look at what it looks like when you open it by double-clicking on your computer…

double-clicking a vCard on Windows (with Outlook installed)

Or opening it in Windows Contacts…

in Windows Contacts (without Outlook)

The contact of this vCard was generated by the Python app (shown below in full) looks like this:

BEGIN:VCARD
VERSION:2.1
N:Claus;Santa;;;
FN:Santa Claus
EMAIL:santa@npole.org
TEL;WORK:(800)-CALL-SANTA
URL;WORK:http://santaclaus.com
END:VCARD

Now that we have a good idea of what’s inside a vCard and how to looks to regular users, let’s dive into the code that created this (and others from a CSV file). The CSV file in this example is a list of 6 contacts with the following format and information:

First Name,Last Name,E-mail,Business Phone,Website
Santa,Claus,santa@npole.org,(800)-CALL-SANTA,http://santaclaus.com
Eye,Society,eyes@eyesociety.com,(425)357-8234,https://www.eyesocietyvision.com/
Mill Creek,Vision,eyes@mcvision.com,(425)745-5650,https://www.millcreekvision.com/
North Creek,Pet hospital,pets@ncph.com,(425)481-9400,https://www.northcreekpethospital.com/
Shuttle express,Service,shuttle@se.com,(425)981-7000,https://shuttleexpress.com/
East Coast,Enzo's Pizza,eastcoastenzos@gmail.com,(425)337-5595,http://www.eastcoastenzos.com/

With this information, our code bring in each row, in the order of the source file, and write the field value for each into vCard format as we’ve seen above. Following is my full source code:

I’ve added comments to call out some specific statements to help you follow along. As we see from the code that a vCard file name is dependent on first name and we iterate through the list in CSV. What if there were two John entries (perhaps two different people with same first names, or same person with two different records)? The last vCard would overwrite any previous one! To prevent that, I added a unique identifier in the vCard file name…which is of course, the record number. That way, if we have a John entry in row 2, and another in row 20, another in 300, we’ll still have all John records.

If we wanted to see record created as vCard in memory before writing to the VCF file, we can print(s) after line 49. The screen output for the above CSV file would look like this:

screen output

After the app ends, the vCard files (VCF extension) are in the designated folder as expected:

Now we can open them in Windows Contacts, import them into Outlook (or other email client) contacts, share them as with any file, attach to email or send a SMS to a phone…since it’s an open standard, it’s widely supported on most devices. Hope you found this VCF<->CSV conversion and custom application solution helpful and interesting.


Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top