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';
Nice i’ve been looking for a way to do this.
Where would I put this script?
Thanks.
-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
Can I have a look at the vcf file?
Sure here is the link to it:
http://test.shared-vision.net/files/Sharedvision.vcf
Thanks!!
-Ann
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.
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
Thanks man, I was planning to write it myself, came across yours and liked it.
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?
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/
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.
Thanks!
There are many solutions importing/exporting/converting vcf files but for this simple procedure is hard to fine.
Thanks!
guyzzz leave all ,,, just download this and extract , then read txt file in it .
http://www.mediafire.com/?y26qw3h2akh1x0i
Enjoy !!!
for the last poster: excellent thank you – worked like a charm!!!!!
Thanks nice job done
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.
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