Skip to content

Instantly share code, notes, and snippets.

@wzhd
Forked from senko/vcf2csv.py
Last active August 29, 2015 14:17
Show Gist options
  • Save wzhd/ed90fcd6f741c8543804 to your computer and use it in GitHub Desktop.
Save wzhd/ed90fcd6f741c8543804 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Parse phone and email records out of vCard file and store them in a CSV.
Copyright (C) 2012 Senko Rasic <senko.rasic@dobarkod.hr>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import csv
import sys
import logging
import vobject
log = logging.getLogger(__name__)
def sanitize_phone(num, country_prefix):
"""
Convert phone numbers into standard +<ccode><phonenum> format, without
spaces, dashes or slashes. The default country prefix used is +385
(Croatia).
"""
orig = num
num = num.replace(' ', '').replace('-', '').replace('/', '')
if num.startswith('0'):
if num.startswith('00'):
num = '+' + num[2:]
else:
num = '+' + country_prefix + num[1:]
elif not num.startswith('+') and len(num) > 5:
num = '+' + num
if num != orig:
log.debug('[sanitize_phone] %s -> %s' % (orig, num))
return num
def parse(fname, country_prefix):
"""
Parse vCard file. Returns a generator producing dicts with 'name' (string),
'emails' (list of strings) and 'phones' (list of strings) fields.
"""
for vobj in vobject.readComponents(open(fname)):
if not hasattr(vobj, 'fn'):
log.warning('[parse] skipping entry with no name: ' + repr(vobj))
continue
entry = {
'name': vobj.fn.value.strip(),
'emails': [],
'phones': [],
'websites': []
}
if hasattr(vobj, 'email_list'):
entry['emails'] = [e.value.strip() for e in vobj.email_list]
if hasattr(vobj, 'tel_list'):
entry['phones'] = [sanitize_phone(t.value, country_prefix)
for t in vobj.tel_list]
if hasattr(vobj, 'x_socialprofile_list'):
entry['websites'] = [w.value.strip() for w in vobj.x_socialprofile_list]
log.debug('[parse] got entry for ' + entry['name'])
yield entry
def csv_output(fname, entries):
"""
Write the entries generated by parse() to a UTF-8 encoded CSV file with
three columns: Name, Phones and Emails.
"""
with open(fname, 'wb') as fd:
writer = csv.writer(fd)
writer.writerow(['Name', 'Phones', 'Emails', 'Websites'])
for e in entries:
writer.writerow([
e['name'].encode('utf-8'),
','.join(e['phones']),
','.join(e['emails']),
','.join(e['websites'])
])
def vcf2csv(infile, outfile, country_prefix):
"""
Parse entries out of vCard 'infile' and store them to CSV 'outfile'.
"""
csv_output(outfile, parse(infile, country_prefix))
if __name__ == '__main__':
if len(sys.argv) != 3 and len(sys.argv) != 4:
print "Usage: %s <infile.vcf> <outfile.csv> [<prefix>]" % sys.argv[0]
print "Default country prefix: 385 (Croatia)"
sys.exit(-1)
logging.basicConfig(level=logging.INFO)
vcf2csv(sys.argv[1], sys.argv[2],
sys.argv[3] if len(sys.argv) == 4 else '385')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment