Skip to content

Instantly share code, notes, and snippets.

@pe7er
Created November 22, 2023 12:27
Show Gist options
  • Save pe7er/43c910a89dd2d45c32a0f0853fa80ac8 to your computer and use it in GitHub Desktop.
Save pe7er/43c910a89dd2d45c32a0f0853fa80ac8 to your computer and use it in GitHub Desktop.
Convert names + email addresses from a VCF to a CSV file
import pandas as pd
import vobject
def vcf_to_csv(vcf_file, csv_file):
with open(vcf_file, 'r', encoding='utf-8') as file:
vcf_data = file.read()
# Parse the VCF data using vobject
vcf_parsed = vobject.readComponents(vcf_data)
# Extract information from the VCF objects
data_list = []
for vcard in vcf_parsed:
name = vcard.fn.value if hasattr(vcard, 'fn') else ''
email = vcard.email.value if hasattr(vcard, 'email') else ''
data_list.append({'Name': name, 'Email': email})
# Create a DataFrame from the extracted data
df = pd.DataFrame(data_list)
# Write the DataFrame to a CSV file
df.to_csv(csv_file, index=False)
# Example usage
vcf_to_csv('/path/to/contacts.vcf', '/path/to/contacts.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment