-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcard_maker.py
70 lines (56 loc) · 2.24 KB
/
vcard_maker.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
from openpyxl import load_workbook
class Contact():
def __init__(self, first_name, last_name, phone, id):
self.first_name = first_name
self.last_name = last_name
self.phone = phone
self.id = id
def clear_vcard(id):
try:
file = open(f"./Contacts/Contacts {id}.vcf", "w")
file.close()
except FileNotFoundError:
print(f"Missing \"{folder_name}\" folder in current directory")
quit()
def print_vcard(contact):
file.write("BEGIN:VCARD\n")
file.write("VERSION:2.1\n")
file.write(f"N:{contact.last_name};{contact.first_name};;;\n")
file.write(f"FN:{contact.first_name} {contact.last_name}\n")
file.write(f"TEL;CELL:{contact.phone}\n")
file.write("END:VCARD\n")
# Main
wb = load_workbook("contact_list.xlsx")
ws = wb["Contacts"]
folder_name = "Contacts" # Folder must be created in the main directory. Used to store vCards
id_col = 1 # This column will provide the name for the .vcf file
first_name_col = 2
last_name_col = 3
phone_col = 4
num_of_contacts = 14 # Number of contacts to add per vCard. Modify as required
num_of_ids = int((ws.max_row - 1) // num_of_contacts)
row = 2
for i in range(num_of_ids):
id = int(ws.cell(row, id_col).value)
clear_vcard(id)
for j in range(row, row + num_of_contacts):
try:
with open(f"./{folder_name}/Contacts {id}.vcf", "a") as file:
first_name = ws.cell(j, first_name_col).value
last_name = ws.cell(j, last_name_col).value
# Convert empty cells into blank text
if first_name is None:
first_name = ""
if last_name is None:
last_name = ""
# Convert invalid phone number into blank text
try:
phone = int(ws.cell(j, phone_col).value)
except TypeError:
phone = ""
contact = Contact(first_name, last_name, phone, id)
print_vcard(contact)
except FileNotFoundError:
print(f"Missing \"{folder_name}\" folder in current directory")
quit()
row += num_of_contacts