Created
September 10, 2012 11:20
-
-
Save diyan/3690398 to your computer and use it in GitHub Desktop.
Converts exported CSV-file from http://www.easyprograms.narod.ru/PhoneBook/Main.html program into vCard format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import csv | |
import os | |
import codecs | |
import unicodedata | |
import vobject | |
from vobject.vcard import Name, Address | |
input_path = u'D:/projects/csv_to_vcard/phonebook_exported_20120910.csv' | |
output_dir = u'D:/projects/csv_to_vcard/output_20120910/' | |
def utf_8_encoder(unicode_csv_content): | |
for line in unicode_csv_content.split('\r\n'): | |
yield line.encode('utf8') | |
def normalize_name(name): | |
if is_cyrillic_chars(name): | |
return name.capitalize() | |
return name | |
def is_cyrillic_chars(name): | |
if not name: | |
return False | |
for unicode_char in name: | |
if not unicodedata.name(unicode_char).startswith('CYRILLIC'): | |
return False | |
return True | |
def map_phonebook_row_to_vcard(utf8_row): | |
try: | |
unicode_row = [unicode(line, 'utf8') for line in utf8_row] | |
first_name, last_name, middle_name, home_phone, mobile_phone, work_phone, \ | |
email, birthday, age, address, note, group = unicode_row | |
first_name = normalize_name(first_name) | |
last_name = normalize_name(last_name) | |
middle_name = normalize_name(middle_name) | |
card = vobject.vCard() | |
card.add('n') | |
card.n.value = Name(family=last_name, given=first_name, additional=middle_name) | |
card.add('fn') | |
card.fn.value = u'{0} {1}'.format(first_name, last_name).strip() | |
card.add('email') | |
card.email.value = email | |
card.email.type_param = 'INTERNET' | |
card.add('adr') | |
card.adr.value = Address(street=address) | |
card.add('label') | |
card.label.value = group | |
work_phone_line = card.add('tel') | |
work_phone_line.value = work_phone | |
work_phone_line.type_param = 'WORK' | |
home_phone_line = card.add('tel') | |
home_phone_line.value = home_phone | |
home_phone_line.type_param = 'HOME' | |
mobile_phone_line = card.add('tel') | |
mobile_phone_line.value = mobile_phone | |
mobile_phone_line.type_param = 'CELL' | |
card.add('bday') | |
card.bday.value = birthday #datetime.strptime(birthday, format='') | |
card.add('note') | |
card.note.value = note | |
#add also age value somehow. | |
return card | |
except Exception as ex: | |
print 'Failed convert phonebook row into vcard, error: {0}, row content: {1}'.format( | |
ex.message, utf8_row) | |
raise Exception('Failed convert phonebook row into vcard, error: {0}, row content: {1}'.format( | |
ex.message, utf8_row), ex) | |
with codecs.open(input_path, encoding='cp1251') as input_file: | |
csv_content = input_file.read() | |
# csv module can not parse unicode content, so we will encode it into utf8. | |
csv_reader = csv.reader(utf_8_encoder(csv_content), delimiter=',', quoting=csv.QUOTE_ALL) | |
header_line = csv_reader.next() | |
cards = [map_phonebook_row_to_vcard(utf8_row) for utf8_row in csv_reader if utf8_row] | |
for card in cards: | |
card_path = os.path.join(output_dir, u'{0}.vcf'.format(card.fn.value)) | |
with open(card_path, 'wb') as card_file: | |
a = card.serialize() | |
card_file.truncate(0) | |
card_file.write(card.serialize()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment