-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcf2csv.py
40 lines (32 loc) · 1.18 KB
/
vcf2csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
import vobject
import argparse
import unicodecsv as csv
parser = argparse.ArgumentParser(description='Convert vCard file to CSV file')
parser.add_argument("input", help="the vCard file name")
parser.add_argument("output", help="the CSV file name")
args = parser.parse_args()
input_file = args.input
output_file = args.output
csv_file = open(output_file, 'wb')
writer = csv.writer(csv_file, encoding='utf-8-sig')
writer.writerow(['First Name', 'Work Tel', 'Mobile Tel', 'CUSTOM EMAIL', 'HOME EMAIL'])
fields = ["fn", "tel", "email"]
with open(input_file, 'r', encoding='utf-8-sig') as f:
data = f.read()
vcards = vobject.readComponents(data)
for vcard in vcards:
row = []
for field in fields:
try:
row.append(vcard.contents[field][0].value)
if field == 'email' or field == 'tel':
try:
row.append(vcard.contents[field][1].value)
except:
row.append('')
continue
except:
row += ['', '']
writer.writerow(row)
csv_file.close()