Created
January 19, 2018 00:41
-
-
Save dwinston/fa3f0e68623ca57653aeb56aad3eefe9 to your computer and use it in GitHub Desktop.
Convert a CSV file of people to vCard files for updating iOS contacts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Convert a CSV file of the form | |
``` | |
name, email, cell | |
First Last, [email protected], 800-555-1234 | |
... | |
``` | |
to vCard (vcf) files suitable for creating/updating iOS contacts. | |
Based on https://codereview.stackexchange.com/questions/3517/converting-a-csv-to-vcards-in-python | |
but updated for Python 3 and for my use case. | |
""" | |
import csv | |
def convert(file): | |
reader = csv.DictReader(file) | |
template = """\ | |
BEGIN:VCARD | |
VERSION:3.0 | |
N:{last};{first} | |
FN:{first} {last} | |
EMAIL;type=INTERNET;type=HOME;type=pref:{email} | |
TEL;type=CELL;type=VOICE;type=pref:{cell} | |
END:VCARD\ | |
""" | |
for row in reader: | |
first, last = row['name'].split() | |
email = row['email'] | |
cell = row['cell'] | |
data = dict(first=first, last=last, email=email, cell=cell) | |
with open("{first} {last}.vcf".format(**data), "w") as f: | |
f.write(template.format(**data)) | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser( | |
description="Convert CSV to a bunch of vCard files.") | |
parser.add_argument( | |
"file", type=argparse.FileType("r"), help="CSV file to convert") | |
args = parser.parse_args() | |
convert(args.file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment