Splitting multiple-vCards-file to one-vCard-per-file

vCard is a standard format, defined for electronic business cards. vCards contain name, phone numbers, address, email address, URLs, photograph etc. These are stored as plain-text files of extension vcf or vcard.

Usually, one vCard file contains details of a single contact. But there are variations in its implementation. A few applications support multiple contacts in a single vCard file. GMail, Mac OS X and KDE Kontact are such applications. Microsoft Outlook and Symbian on the other hand allows only one contact per file. If you try to import a vCard file, with multiple contacts, they will import only the first contact.

I needed to convert single file containing multiple contacts to one-contact-per-file. For the same, I wrote a Python script. This script splits a vCard file to multiple files containing one contact each. This will help moving contacts to the applications which don’t support multiple contacts per file.

Example of vCard format

BEGIN:VCARD
VERSION:3.0
FN:Manjeet Dahiya
N:Dahiya;Manjeet;;;
EMAIL;TYPE=INTERNET:xxx@gmail.com
TEL;TYPE=CELL:xxx
END:VCARD

BEGIN:VCARD
VERSION:3.0
FN:Manjeet Dahiya
N:Dahiya;Manjeet;;;
EMAIL;TYPE=INTERNET:xxx@gmail.com
TEL;TYPE=CELL:xxx
END:VCARD
BEGIN:VCARD
VERSION:3.0
FN:Shivraj Singh
N:Singh;Shivraj;;;
EMAIL;TYPE=INTERNET:xxx@gmail.com
TEL;TYPE=CELL:xxx
END:VCARD

Script Usage

python vCardSplit <vcard-file>
import sys

if (len(sys.argv) != 2):
 print "Usage: vCardSplit <vcard file>\n"
 sys.exit()

vCardFilePath = sys.argv[1]

vCardFile = open(vCardFilePath, 'r')

a = 0
vcardStarted = False
while True:
 line = vCardFile.readline()
 if not line:
 break

 if line.startswith('BEGIN:VCARD'):
 vcardStarted = True
 a = a + 1
 splitFile = open('contact_' + str(a) + '.vcf', 'w')

 if vcardStarted:
 splitFile.write(line)

 if line.startswith('END:VCARD'):
 vcardStarted = False
 splitFile.close()

vCardFile.close()

print 'Generated ' + str(a) + ' vCards';

17 thoughts on “Splitting multiple-vCards-file to one-vCard-per-file

  1. Ann K

    Nice i’ve been looking for a way to do this.

    Where would I put this script?

    Thanks.
    -Ann K

    Reply
  2. Ann K

    Okay I put the script in the vcf file after the vcf info.

    It does generate two vcfs but it copies the first ones information creating two of the same card.
    I tried this in gmail and Outlook 2011 and had the same result.

    Any assistance would be appreciated.

    Thanks.
    -Ann

    Reply
        1. Manjeet Dahiya Post author

          The way you are doing is incorrect. You have to create a separate python script file with the name: vCardSplit.py and copy the source code its source code into it. Remove the python code from Sharedvision.vcf file.

          * Then put both the files vCardSplit.py and Sharedvision.vcf in the same directory.
          * Open the command line and go the directory where above files are present
          * Then call the following command:
          python vCardSplit.py Sharedvision.vcf

          You need to have Python installed too.

          Reply
  3. Ann K

    Thank You for you help with this but this is a little beyond my skill set.

    I did the first step of what you have responded with.
    I am not sure how to do the next one or last one.

    Also I have a hosting package that is on a grid server and I’m not sure how to go about putting python on there so I will have to ask the hosting company about that first.

    Again thanks for your help! I hope I can figure this out.

    -Ann

    Reply
  4. alexey_laa

    I’d like to split my file, exported from Google contacts with your script, but got an error while the script was executed:

    File "vCardSplit.py", line 16
    break
    ^
    IndentationError: expected an indented block

    Could you explain, please, how to run your script without this error?

    Reply
  5. Sean

    To the previous poster: the indentation error seems to be due to the formatting of his script on the site here. If you go through the script and replace the single spaces with a single tab, it will run just fine. Also, you’ll need to tab twice sometimes, in order to correctly indent the conditional statements.

    Check the PEP-8 document for details on formatting:

    http://www.python.org/dev/peps/pep-0008/

    Reply
  6. James Jordan

    Just wanted to say thanks, your script saved me a bunch of time and worked perfectly. As the previous poster noted, the indentation changes are necessary before the python script will work, but other than that it was great. Perhaps you should offer a downloadable copy of the source .py file, so non-programmers could just download it? Just a suggestion, but regardless, I really appreciated your post.

    Reply
  7. Lajos Demmiany

    Thanks!
    There are many solutions importing/exporting/converting vcf files but for this simple procedure is hard to fine.
    Thanks!

    Reply
  8. Jeff

    I had the same indentation error that someone else posted about, just wanted to share how I fixed it.

    Python determines blocks of code by their indentation. Unfortunately, whitespace isn’t always preserved when posting on a blog. For me, line 16 (“break”) needed to be indented further in than the line just before it. So:

    if not line:
    ____break

    Of course, replace the underscores with spaces — four of them.

    Hope that helps.

    Reply
  9. rexx123456

    Thank you so much, “mere.bapu ” for your program!

    Now I can split my .vcf files. It’s fast, easy to use and works perfectly!!

    Tested on Windows XP Professional 32bit, Service Pack 3 with Outlook 2007.

    Rexx123456

    Reply
  10. Richard

    Hi,

    Thank you for this!

    How could we make this script to name the resulting files using the ‘FN:’ (full name) line of each contact?
    (E.g. john-taylor.vcf instead of ‘contact_n.vcf’)
    It seems complex, as the full name would have to be:
    – (maybe) converted to ASCII
    – (maybe) converted to lowercase
    – all spaces converted to dashes or underscores
    – append an incrementing number if there is already a file in the folder with the same converted name

    Bahh, this is getting too complicated :)

    Reply

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>