convert_to_vcard.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env python
  2. import csv
  3. import phonenumbers
  4. with open('../../Documents/Contacts/contacts.csv') as f:
  5. reader = csv.DictReader(f)
  6. for row in reader:
  7. # print(row['Given Name'])
  8. contents = 'BEGIN:VCARD\r\n'
  9. contents += 'VERSION:4.0\r\n'
  10. # Name
  11. if not row['Given Name'] == '':
  12. # print(row['Given Name'])
  13. contents += 'KIND:individual\r\n'
  14. contents += 'FN:{}\r\n'.format(row['Name'])
  15. contents += 'N:{};{};{};{};{}\r\n'.format(
  16. row['Family Name'],
  17. row['Given Name'],
  18. row['Additional Name'],
  19. row['Name Prefix'],
  20. row['Name Suffix'],
  21. )
  22. else:
  23. contents += 'KIND:org\r\n'
  24. contents += 'FN:{}\r\n'.format(row['Organization 1 - Name'])
  25. # Phone number
  26. numbers = []
  27. if len(row['Phone 1 - Value']) == 3:
  28. contents += 'TEL;VALUE=uri;TYPE=voice:tel:{}\r\n'.format(row['Phone 1 - Value'])
  29. elif row['Phone 1 - Value'][:4] == '1800' or row['Phone 1 - Value'][:4] == '1855':
  30. contents += 'TEL:VALUE=uri;TYPE=voice:tel:{}\r\n'.format(row['Phone 1 - Value'].replace(' ', '').replace('-', ''))
  31. elif row['Phone 1 - Value'][:1] == '*' and row['Phone 1 - Value'][-1:] == '#':
  32. contents += 'TEL:VALUE=uri;TYPE=ussd:tel:{}\r\n'.format(row['Phone 1 - Value'])
  33. else:
  34. for number in row['Phone 1 - Value'].split(' ::: '):
  35. if not number == '':
  36. number_parsed = phonenumbers.parse(number)
  37. number_rfc3966 = phonenumbers.format_number(number_parsed, phonenumbers.PhoneNumberFormat.RFC3966)
  38. number_type = False
  39. if row['Phone 1 - Type'] == 'Mobile':
  40. number_type = 'cell'
  41. elif row['Phone 1 - Type'] == 'Work' or row['Phone 1 - Type'] == 'workFax' or row['Phone 1 - Type'] == 'Pager' or row['Phone 1 - Type'] == 'homeFax':
  42. number_type = 'voice,work'
  43. elif row['Phone 1 - Type'] == 'Voicemail':
  44. number_type = 'voice,voicemail'
  45. elif row['Phone 1 - Type'] == 'Main' or row['Phone 1 - Type'] == 'Other':
  46. number_type = 'voice'
  47. elif row['Phone 1 - Type'] == 'Home':
  48. number_type = 'voice,landline'
  49. if number_type:
  50. if ',' in number_type:
  51. contents += 'TEL;VALUE=uri;TYPE="{teltype}":{tel}\r\n'.format(tel=number_rfc3966, teltype=number_type)
  52. else:
  53. contents += 'TEL;VALUE=uri;TYPE={teltype}:{tel}\r\n'.format(tel=number_rfc3966, teltype=number_type)
  54. else:
  55. contents += 'TEL;VALUE=uri;TYPE=voice:{}\r\n'.format(number_rfc3966)
  56. if len(row['Phone 2 - Value']) == 3:
  57. contents += 'TEL;VALUE=uri;TYPE=voice:tel:{}\r\n'.format(row['Phone 2 - Value'])
  58. elif row['Phone 2 - Value'][:4] == '1800' or row['Phone 2 - Value'][:4] == '1855':
  59. contents += 'TEL:VALUE=uri;TYPE=voice:tel:{}\r\n'.format(row['Phone 2 - Value'].replace(' ', '').replace('-', ''))
  60. elif row['Phone 2 - Value'][:1] == '*' and row['Phone 2 - Value'][-1:] == '#':
  61. contents += 'TEL:VALUE=uri;TYPE=ussd:tel:{}\r\n'.format(row['Phone 2 - Value'])
  62. else:
  63. for number in row['Phone 2 - Value'].split(' ::: '):
  64. if not number == '':
  65. number_parsed = phonenumbers.parse(number)
  66. number_rfc3966 = phonenumbers.format_number(number_parsed, phonenumbers.PhoneNumberFormat.RFC3966)
  67. number_type = False
  68. if row['Phone 2 - Type'] == 'Mobile':
  69. number_type = 'cell'
  70. elif row['Phone 2 - Type'] == 'Work' or row['Phone 2 - Type'] == 'workFax' or row['Phone 2 - Type'] == 'Pager' or row['Phone 2 - Type'] == 'homeFax':
  71. number_type = 'voice,work'
  72. elif row['Phone 2 - Type'] == 'Voicemail':
  73. number_type = 'voice,voicemail'
  74. elif row['Phone 2 - Type'] == 'Main' or row['Phone 2 - Type'] == 'Other':
  75. number_type = 'voice'
  76. elif row['Phone 2 - Type'] == 'Home':
  77. number_type = 'voice,landline'
  78. if number_type:
  79. if ',' in number_type:
  80. contents += 'TEL;VALUE=uri;TYPE="{teltype}":{tel}\r\n'.format(tel=number_rfc3966, teltype=number_type)
  81. else:
  82. contents += 'TEL;VALUE=uri;TYPE={teltype}:{tel}\r\n'.format(tel=number_rfc3966, teltype=number_type)
  83. else:
  84. contents += 'TEL;VALUE=uri;TYPE=voice:{}\r\n'.format(number_rfc3966)
  85. if len(row['Phone 3 - Value']) == 3:
  86. contents += 'TEL;VALUE=uri;TYPE=voice:tel:{}\r\n'.format(row['Phone 3 - Value'])
  87. elif row['Phone 3 - Value'][:4] == '1800' or row['Phone 3 - Value'][:4] == '1855':
  88. contents += 'TEL:VALUE=uri;TYPE=voice:tel:{}\r\n'.format(row['Phone 3 - Value'].replace(' ', '').replace('-', ''))
  89. elif row['Phone 3 - Value'][:1] == '*' and row['Phone 3 - Value'][-1:] == '#':
  90. contents += 'TEL:VALUE=uri;TYPE=ussd:tel:{}\r\n'.format(row['Phone 3 - Value'])
  91. else:
  92. for number in row['Phone 3 - Value'].split(' ::: '):
  93. if not number == '':
  94. number_parsed = phonenumbers.parse(number)
  95. number_rfc3966 = phonenumbers.format_number(number_parsed, phonenumbers.PhoneNumberFormat.RFC3966)
  96. number_type = False
  97. if row['Phone 3 - Type'] == 'Mobile':
  98. number_type = 'cell'
  99. elif row['Phone 3 - Type'] == 'Work' or row['Phone 3 - Type'] == 'workFax' or row['Phone 3 - Type'] == 'Pager' or row['Phone 3 - Type'] == 'homeFax':
  100. number_type = 'voice,work'
  101. elif row['Phone 3 - Type'] == 'Voicemail':
  102. number_type = 'voice,voicemail'
  103. elif row['Phone 3 - Type'] == 'Main' or row['Phone 3 - Type'] == 'Other':
  104. number_type = 'voice'
  105. elif row['Phone 3 - Type'] == 'Home':
  106. number_type = 'voice,landline'
  107. if number_type:
  108. if ',' in number_type:
  109. contents += 'TEL;VALUE=uri;TYPE="{teltype}":{tel}\r\n'.format(tel=number_rfc3966, teltype=number_type)
  110. else:
  111. contents += 'TEL;VALUE=uri;TYPE={teltype}:{tel}\r\n'.format(tel=number_rfc3966, teltype=number_type)
  112. else:
  113. contents += 'TEL;VALUE=uri;TYPE=voice:{}\r\n'.format(number_rfc3966)
  114. if len(row['Phone 4 - Value']) == 3:
  115. contents += 'TEL;VALUE=uri;TYPE=voice:tel:{}\r\n'.format(row['Phone 4 - Value'])
  116. elif row['Phone 4 - Value'][:4] == '1800' or row['Phone 4 - Value'][:4] == '1855':
  117. contents += 'TEL:VALUE=uri;TYPE=voice:tel:{}\r\n'.format(row['Phone 4 - Value'].replace(' ', '').replace('-', ''))
  118. elif row['Phone 4 - Value'][:1] == '*' and row['Phone 4 - Value'][-1:] == '#':
  119. contents += 'TEL:VALUE=uri;TYPE=ussd:tel:{}\r\n'.format(row['Phone 4 - Value'])
  120. else:
  121. for number in row['Phone 4 - Value'].split(' ::: '):
  122. if not number == '':
  123. number_parsed = phonenumbers.parse(number)
  124. number_rfc3966 = phonenumbers.format_number(number_parsed, phonenumbers.PhoneNumberFormat.RFC3966)
  125. number_type = False
  126. if row['Phone 4 - Type'] == 'Mobile':
  127. number_type = 'cell'
  128. elif row['Phone 4 - Type'] == 'Work' or row['Phone 4 - Type'] == 'workFax' or row['Phone 4 - Type'] == 'Pager' or row['Phone 4 - Type'] == 'homeFax':
  129. number_type = 'voice,work'
  130. elif row['Phone 4 - Type'] == 'Voicemail':
  131. number_type = 'voice,voicemail'
  132. elif row['Phone 4 - Type'] == 'Main' or row['Phone 4 - Type'] == 'Other':
  133. number_type = 'voice'
  134. elif row['Phone 4 - Type'] == 'Home':
  135. number_type = 'voice,landline'
  136. if number_type:
  137. if ',' in number_type:
  138. contents += 'TEL;VALUE=uri;TYPE="{teltype}":{tel}\r\n'.format(tel=number_rfc3966, teltype=number_type)
  139. else:
  140. contents += 'TEL;VALUE=uri;TYPE={teltype}:{tel}\r\n'.format(tel=number_rfc3966, teltype=number_type)
  141. else:
  142. contents += 'TEL;VALUE=uri;TYPE=voice:{}\r\n'.format(number_rfc3966)
  143. contents += 'END:VCARD'
  144. print(contents)
  145. print()