Home > Blockchain >  How can I generate multiple Vcard QRCodes from a CSV-File in Python?
How can I generate multiple Vcard QRCodes from a CSV-File in Python?

Time:11-08

> lastname,firstname,org,title,phone,email,website,street,city,p_code,country
> Doe,John,John Doe plc,Web Developer,143893456,[email protected],https://johndoe.com, 203 East 50th Steet,New York,10022,USA 
> Morgan,Peter,Pythonfactory Inc.,Backend Developer,141996746,[email protected],https://pythonfactory.com,203 Weststeet,New York,10022,USA



import pyqrcode
import pandas as pd

def createQRCode():
    df = pd.read_csv("havas.csv")

    for index, values in df.iterrows():
        lastname = values["lastname"]
        firstname = values["firstname"]
        title = values["title"]
        phone = values["phone"]
        email = values["email"]
        website = values["website"]
        org = values["org"]
        street = values["street"]
        city = values["city"]
        p_code = values["p_code"]
        country = values["country"]

        data = f'''
        "BEGIN:VCARD\n"
        "N:{lastname};{firstname};\n"
        "FN:{lastname} {firstname}\n"
        "TITLE:{title}\n"
        "TEL;TYPE=work,VOICE:{phone}\n"
        "EMAIL;WORK;INTERNET:{email}\n"
        "URL:{website}\n"
        "ORG:{org}\n"
        "ADR;TYPE=work,PREF;;;{street};{city};{p_code};{country}\n"
        "VERSION:3.0\n"
        "END:VCARD\n"
        '''

        image = pyqrcode.create(data)
        image.svg(f"{lastname}_{firstname}.svg", scale="5")

createQRCode()

I have a CSV file which contains several employees. From this file I would like to generate a Vcard QR code for each employee. Unfortunately, only the URL is retrieved when the QRCode is scanned.

Unfortunately, I do not recognise the error, as I am inexperienced in Python. I would be very grateful for your help!

CodePudding user response:

As you are using a Python multiline string, you do not also need to include newlines, extra quotes and indentation. Try the following:

import pyqrcode
import pandas as pd

def createQRCode():
    df = pd.read_csv("havas.csv")

    for index, values in df.iterrows():
        lastname = values["lastname"]
        firstname = values["firstname"]
        title = values["title"]
        phone = values["phone"]
        email = values["email"]
        website = values["website"]
        org = values["org"]
        street = values["street"]
        city = values["city"]
        p_code = values["p_code"]
        country = values["country"]

        data = f'''BEGIN:VCARD
N:{lastname};{firstname};
FN:{lastname} {firstname}
TITLE:{title}
TEL;TYPE=work,VOICE:{phone}
EMAIL;WORK;INTERNET:{email}
URL:{website}
ORG:{org}
ADR;TYPE=work,PREF;;;{street};{city};{p_code};{country}
VERSION:3.0
END:VCARD'''

        image = pyqrcode.create(data)
        image.svg(f"{lastname}_{firstname}.svg", scale="5")

createQRCode()

This would create the following QR code:

QR code from CSV file entry

You could do the following to avoid the need for extra variables:

import pyqrcode
import pandas as pd

def createQRCode():
    df = pd.read_csv("havas.csv")

    for index, v in df.iterrows():

        data = f'''BEGIN:VCARD
N:{v['lastname']};{v['firstname']};
FN:{v['lastname']} {v['firstname']}
TITLE:{v['title']}
TEL;TYPE=work,VOICE:{v['phone']}
EMAIL;WORK;INTERNET:{v['email']}
URL:{v['website']}
ORG:{v['org']}
ADR;TYPE=work,PREF;;;{v['street']};{v['city']};{v['p_code']};{v['country']}
VERSION:3.0
END:VCARD'''

        image = pyqrcode.create(data)
        image.svg(f"{v['lastname']}_{v['firstname']}.svg", scale="5")

createQRCode()
  • Related