Se você tem um CSV com um cabeçalho como
csv_header_example.txt
"Email Address","First Name","Last Name",MEMBER_RATING,OPTIN_TIME,OPTIN_IP,CONFIRM_TIME,CONFIRM_IP,LATITUDE,LONGITUDE,GMTOFF,DSTOFF,TIMEZONE,CC,REGION,LAST_CHANGED,LEID,EUID,NOTESvocê pode usar o seguinte script Python para convertê-lo em um diretório de arquivos vcard:
csv_to_vcard.py
#!/usr/bin/env python3
import csv
import os
import argparse
# Configurar o parser de argumentos
parser = argparse.ArgumentParser(description='Convert a CSV file of contacts to vCard files.')
parser.add_argument('csv_file', type=str, help='Path to the CSV file containing contacts')
args = parser.parse_args()
# Definir o caminho para o arquivo CSV a partir do argumento de linha de comando
csv_file_path = args.csv_file
# Criar um diretório para armazenar os arquivos vCard
vcard_directory = 'vcards'
os.makedirs(vcard_directory, exist_ok=True)
# Ler o arquivo CSV e processar cada contato
with open(csv_file_path, mode='r', encoding='utf-8') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
# Extrair os detalhes do contato
email = row['Email Address']
first_name = row['First Name']
last_name = row['Last Name']
latitude = row['LATITUDE']
longitude = row['LONGITUDE']
notes = row['NOTES']
# Criar o conteúdo vCard
vcard_content = (
f"BEGIN:VCARD\n"
f"VERSION:3.0\n"
f"N:{last_name};{first_name};;;\n"
f"FN:{first_name} {last_name}\n"
f"EMAIL;TYPE=INTERNET:{email}\n"
)
if latitude and longitude:
vcard_content += f"GEO:{latitude};{longitude}\n"
if notes:
vcard_content += f"NOTE:{notes}\n"
vcard_content += "END:VCARD\n"
# Definir o nome do arquivo para o vCard
vcard_file_name = f"{first_name}_{last_name}.vcf".replace(" ", "_")
vcard_file_path = os.path.join(vcard_directory, vcard_file_name)
# Escrever o conteúdo vCard no arquivo
with open(vcard_file_path, mode='w', encoding='utf-8') as vcard_file:
vcard_file.write(vcard_content)
print(f"Os arquivos vCard foram criados no diretório '{vcard_directory}'.")
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow