### Select all contacts in Contacts.app, select export to vCard.
### It will generate one big file with all contacts.
### [MAKE SURE TO MAKE A BACKUP OF THIS CONTACTS.VCF FILE!!]
### When finished, delete all contacts then import "output.vcf" into Contacts.app.
### BEFORE EXAMPLE
### First Name: Appleseed
### Last Name: John (Apple)
### Company:
### AFTER EXAMPLE:
### First Name: John
### Last Name: Appleseed
### Company: Apple
input_file_path = "edit.vcf"
output_file_path = "output.vcf"
with open(input_file_path, "r") as input_file:
file_content = input_file.read()
to_delete = []
file_content = file_content.split("\n")
for i in range(len(file_content)):
if "N:" in file_content[i] and "FN:" not in file_content[i] and "ON:" not in file_content[i] and "IN:" not in file_content[i]:
#print(i)
name_str = file_content[i]
name_str = name_str.replace("N:","")
name_str = name_str.split(";")
print(name_str)
for j in range(len(name_str)):
name_str[j] = name_str[j].strip()
temp = name_str[0]
temp = temp.replace(")", "")
temp = temp.split("(")
print(temp)
if len(temp) > 1:
occupation = temp[1]
else:
occupation = ""
name_str[0] = name_str[1]
name_str[1] = temp[0]
name_str = "N:"+name_str[0] + ";" + name_str[1] + ";;;\n"+"ORG:"+occupation
print(name_str)
file_content[i] = name_str
if "FN:" in file_content[i]:
to_delete.append(i)
to_delete = to_delete[::-1]
for i in to_delete:
del file_content[i]
output = "\n".join(file_content)
# Open the output file for writing
with open(output_file_path, "w") as output_file:
output_file.write(output)
print(f"File '{input_file_path}' has been edited and saved as '{output_file_path}'.")