Skip to content

Instantly share code, notes, and snippets.

@rahul1990gupta
Forked from Pullusb/CSV_to_Vcards.py
Created July 28, 2017 04:19
Show Gist options
  • Select an option

  • Save rahul1990gupta/3df29a6e6138a154389b2bb60b45a98f to your computer and use it in GitHub Desktop.

Select an option

Save rahul1990gupta/3df29a6e6138a154389b2bb60b45a98f to your computer and use it in GitHub Desktop.

Revisions

  1. @Pullusb Pullusb created this gist Oct 8, 2015.
    59 changes: 59 additions & 0 deletions CSV_to_Vcards.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    # -*- coding: utf-8 -*-
    #python 3.x

    import csv
    import sys

    #convert a "comma separated values" file to vcf contact cards

    #USAGE:
    #CSV_to_Vcards.py CSV_filename


    def convert(somefile):
    #assuming file format : lastname,firstname,phonenumber,mail
    with open( somefile, 'r' ) as source:
    reader = csv.reader( source )
    allvcf = open('ALL.vcf', 'w')
    i = 0
    for row in reader:
    #write in individual vcf
    vcf = open(row[1] + ' ' + row[0] + ".vcf", 'w')
    vcf.write( 'BEGIN:VCARD' + "\n")
    vcf.write( 'VERSION:3.0' + "\n")
    vcf.write( 'N:' + row[0] + ';' + row[1] + "\n")
    vcf.write( 'FN:' + row[1] + ' ' + row[0] + "\n")
    vcf.write( 'ORG:' + 'ATI' + "\n")
    vcf.write( 'TEL;CELL:' + row[2] + "\n")
    vcf.write( 'EMAIL:' + row[3] + "\n")
    vcf.write( 'END:VCARD' + "\n")
    vcf.write( "\n")
    vcf.close()

    #write in the "ALL.vcf" file
    allvcf.write( 'BEGIN:VCARD' + "\n")
    allvcf.write( 'VERSION:3.0' + "\n")
    allvcf.write( 'N:' + row[0] + ';' + row[1] + "\n")
    allvcf.write( 'FN:' + row[1] + ' ' + row[0] + "\n")
    allvcf.write( 'ORG:' + 'ATI' + "\n")
    allvcf.write( 'TEL;CELL:' + row[2] + "\n")
    allvcf.write( 'EMAIL:' + row[3] + "\n")
    allvcf.write( 'END:VCARD' + "\n")
    allvcf.write( "\n")

    i += 1#counts

    allvcf.close()
    print (str(i) + " vcf cards generated")


    def main(args):
    if len(args) != 2:
    print ( "Usage:")
    print ( args[0] + " filename")
    return

    convert(args[1])

    if __name__ == '__main__':
    main(sys.argv)