Introduction
http://www.google.com/contacts?pli=1 Google Contacts's import function will import vcf files containing vCards from Kontact but has the following problems
Solution
A solution is to use the Python vobject library to parse the file and make the following changes
Procedure
	
	
	
	
							
						
					http://www.google.com/contacts?pli=1 Google Contacts's import function will import vcf files containing vCards from Kontact but has the following problems
- Kontact sort-string gets turned into phonetic name
 - Birthday gets loaded in to the notes section rather than Birthday
 - x-kaddressbook-x-anniversary is not loaded
 - x-kaddressbook-x-spousesname is not loaded
 
Solution
A solution is to use the Python vobject library to parse the file and make the following changes
- Firstly - get rid of the custom Kontact fields as vobject cannot handle them
 - Google Contacts cannot load the birthday with a timezone component e.g. BDAY:1973-12-12T00:00:00Z - so remove T00:00:00Z by only including the first 10 digits
 - When we find x-kaddressbook-x-anniversary make an entry ITEM<n>.X-ABDATE with a label "_$!<Anniversary>!$_"
 - When we find x-kaddressbook-x-spousesname make an entry ITEM<n>.X-ABRELATEDNAMES with a label "_$!<Partner>!$_"
 - When we see sort-string - just remove it
 
Procedure
- Pre-process the vcf file by removing custom Kontact fields with awk
 
Code:
	
	awk '{if ($0 !~ /X-KADDRESSBOOK-[^X]/) {print $0}}' starting_file.vcf > intermediate.vcf
- I Had fields like: X-KADDRESSBOOK-birth_weight, X-KADDRESSBOOK-nee. You can see what you are going to remove with:
 
Code:
	
	grep -e X-KADDRESSBOOK-[^X] starting_file.vcf
- Create the following python script file and make it executable
 
Code:
	
	#!/usr/bin/python
import vobject, re, sys
def printSortCard(cardLines):
  # Labels have to come before the value
  for card in cardLines:
    if re.search("^ITEM.\.X-ABLABEL:_\$!<Partner>!\$_", card):
      rememberLabel = card
    else:
      print card
    if re.search("ITEM.\.X-ABRELATEDNAMES", card):
      print rememberLabel
fileName = open(sys.argv[1])
cards = vobject.readComponents(fileName)
for card in cards:
  if 'bday' in card.contents:
    card.bday.value = card.bday.value[:10]
  if 'x-kaddressbook-x-anniversary' in card.contents:
    card.add('ITEM1.X-ABDATE').value = card.x_kaddressbook_x_anniversary.value
    card.add('ITEM1.X-ABLABEL').value = "_$!<Anniversary>!$_"
  if 'x-kaddressbook-x-spousesname' in card.contents:
    card.add('ITEM2.X-ABRELATEDNAMES').value = card.x_kaddressbook_x_spousesname.value
    card.add('ITEM2.X-ABLABEL').value = "_$!<Partner>!$_"
  if 'sort-string' in card.contents:
    # Remove sort-string - phonetic name
    del card.contents[u'sort-string']
  printSortCard(card.serialize(validate=False).splitlines() )
fileName.close()
- If convert_google_vcf.py is in the current directory run
 
Code:
	
	./convert_google_vcf.py intermediate.vcf > google_import.vcf
- Import the file google_import.vcf
 


Comment