-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcsv2vcf.py
180 lines (151 loc) · 6.97 KB
/
csv2vcf.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
Author : Mridul Ahuja
Github : https://github.com/mridah/csv2vcf
Description : A small command line tool to convert CSV files to VCard files
"""
import os
import sys
import csv
import json
def convert_to_vcard(input_file, single_output, input_file_format):
FN = input_file_format['name']-1 if 'name' in input_file_format else None
NICKNAME = input_file_format['nickname']-1 if 'nickname' in input_file_format else None
ORG = input_file_format['org']-1 if 'org' in input_file_format else None
TEL = input_file_format['tel']-1 if 'tel' in input_file_format else None
URL = input_file_format['url']-1 if 'url' in input_file_format else None
BDAY = input_file_format['bday']-1 if 'bday' in input_file_format else None
ROLE = input_file_format['role']-1 if 'role' in input_file_format else None
EMAIL = input_file_format['email']-1 if 'email' in input_file_format else None
NOTE = input_file_format['note']-1 if 'note' in input_file_format else None
# if single output option is selected
if single_output :
with open( input_file, 'r' ) as source_file:
reader = csv.reader( source_file )
single_vcf = open('csv2vcf/all_contacts.vcf', 'w')
i = 0
for row in reader:
FN_VAL = row[FN] if FN is not None else ''
NICKNAME_VAL = row[NICKNAME] if NICKNAME is not None else ''
ORG_VAL = row[ORG] if ORG is not None else ''
TEL_VAL = row[TEL] if TEL is not None else ''
URL_VAL = row[URL] if URL is not None else ''
BDAY_VAL = row[BDAY] if BDAY is not None else ''
ROLE_VAL = row[ROLE] if ROLE is not None else ''
EMAIL_VAL = row[EMAIL] if EMAIL is not None else ''
NOTE_VAL = row[NOTE] if NOTE is not None else ''
print('BEGIN:VCARD')
print('VERSION:3.0')
print('N:' + FN_VAL)
print('FN:' + FN_VAL)
print('NICKNAME:' + NICKNAME_VAL)
print('TEL;HOME;VOICE:' + TEL_VAL)
print('EMAIL:' + EMAIL_VAL)
print('BDAY:' + BDAY_VAL)
print('ORG:' + ORG_VAL)
print('ROLE:' + ROLE_VAL)
print('URL:' + URL_VAL)
print('NOTE:' + NOTE_VAL)
print('END:VCARD')
print('----------------------')
# write the single file
single_vcf.write( 'BEGIN:VCARD' + "\n")
single_vcf.write( 'VERSION:3.0' + "\n")
single_vcf.write( 'N:' + FN_VAL + ';' + "\n")
single_vcf.write( 'FN:' + FN_VAL + "\n")
single_vcf.write( 'NICKNAME:' + NICKNAME_VAL + "\n")
single_vcf.write( 'TEL;HOME;VOICE:' + TEL_VAL + "\n")
single_vcf.write( 'EMAIL:' + EMAIL_VAL + "\n")
single_vcf.write( 'BDAY:' + BDAY_VAL + "\n")
single_vcf.write( 'ORG:' + ORG_VAL + "\n")
single_vcf.write( 'ROLE:' + ROLE_VAL + "\n")
single_vcf.write( 'URL:' + URL_VAL + "\n")
single_vcf.write( 'NOTE:' + NOTE_VAL + "\n")
single_vcf.write( 'END:VCARD' + "\n")
single_vcf.write( "\n")
i += 1
single_vcf.close()
print(str(i) + " VCARDS written")
print('----------------------')
# default ( multi-file output )
else :
with open( input_file, 'r' ) as source_file:
reader = csv.reader( source_file )
i = 0
for row in reader:
FN_VAL = row[FN] if FN is not None else ''
NICKNAME_VAL = row[NICKNAME] if NICKNAME is not None else ''
ORG_VAL = row[ORG] if ORG is not None else ''
TEL_VAL = row[TEL] if TEL is not None else ''
URL_VAL = row[URL] if URL is not None else ''
BDAY_VAL = row[BDAY] if BDAY is not None else ''
ROLE_VAL = row[ROLE] if ROLE is not None else ''
EMAIL_VAL = row[EMAIL] if EMAIL is not None else ''
NOTE_VAL = row[NOTE] if NOTE is not None else ''
print('BEGIN:VCARD')
print('VERSION:3.0')
print('N:' + FN_VAL)
print('FN:' + FN_VAL)
print('NICKNAME:' + NICKNAME_VAL)
print('TEL;HOME;VOICE:' + TEL_VAL)
print('EMAIL:' + EMAIL_VAL)
print('BDAY:' + BDAY_VAL)
print('ORG:' + ORG_VAL)
print('ROLE:' + ROLE_VAL)
print('URL:' + URL_VAL)
print('NOTE:' + NOTE_VAL)
print('END:VCARD')
print('----------------------')
# write each entry
each_vcf = open('csv2vcf/' + FN_VAL + '_' + TEL_VAL + ".vcf", 'w')
each_vcf.write( 'BEGIN:VCARD' + "\n")
each_vcf.write( 'VERSION:3.0' + "\n")
each_vcf.write( 'N:' + FN_VAL + ';' + "\n")
each_vcf.write( 'FN:' + FN_VAL + "\n")
each_vcf.write( 'NICKNAME:' + NICKNAME_VAL + "\n")
each_vcf.write( 'TEL;HOME;VOICE:' + TEL_VAL + "\n")
each_vcf.write( 'EMAIL:' + EMAIL_VAL + "\n")
each_vcf.write( 'BDAY:' + BDAY_VAL + "\n")
each_vcf.write( 'ORG:' + ORG_VAL + "\n")
each_vcf.write( 'ROLE:' + ROLE_VAL + "\n")
each_vcf.write( 'URL:' + URL_VAL + "\n")
each_vcf.write( 'NOTE:' + NOTE_VAL + "\n")
each_vcf.write( 'END:VCARD' + "\n")
each_vcf.write("\n")
each_vcf.close()
i += 1
print(str(i) + " VCARDS written")
print('----------------------')
def main(args):
args_len = len(args)
if args_len < 3 or args_len > 4 :
print ( "Usage:")
print ( args[0] + " filename")
sys.exit()
if args_len == 3 :
input_file = args[1]
try :
input_file_format = json.loads(args[2])
except Exception as e :
print('\033[91m'+"ERROR : json could not be parsed"+'\033[0m')
sys.exit()
single_output = 0
elif args_len == 4 :
input_file = args[1]
if args[2] == '-s' or args[2] == '--single' :
single_output = 1
else :
print('\033[91m'+"ERROR : invalid argument `" + args[2] + "`"+'\033[0m')
sys.exit()
try :
input_file_format = json.loads(args[3])
except Exception as e :
print('\033[91m'+"ERROR : json could not be parsed"+'\033[0m')
sys.exit()
if not os.path.exists(input_file) :
print('\033[91m'+"ERROR : file `" + input_file + "` not found"+'\033[0m')
sys.exit()
if not os.path.exists('csv2vcf') :
os.makedirs('csv2vcf')
convert_to_vcard(input_file, single_output, input_file_format)
if __name__ == '__main__':
main(sys.argv)