Skip to content

Instantly share code, notes, and snippets.

@dwinston
Created January 19, 2018 00:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwinston/fa3f0e68623ca57653aeb56aad3eefe9 to your computer and use it in GitHub Desktop.
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
"""
Convert a CSV file of the form
```
name, email, cell
First Last, bob@example.com, 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