如何从 Python 中的 CSV 文件生成多个 Vcard QRCode?

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

> lastname,firstname,org,title,phone,email,website,street,city,p_code,country
> Doe,John,John Doe plc,Web Developer,143893456,john.doe@hjd.com,https://johndoe.com, 203 East 50th Steet,New York,10022,USA 
> Morgan,Peter,Pythonfactory Inc.,Backend Developer,141996746,peter.morgan@hpythonfactory.com,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()

我有一个包含多个员工的 CSV 文件。我想从这个文件中为每个员工生成一个 Vcard QR 码。不幸的是,扫描 QRCode 时只检索到 URL。

不幸的是,我没有意识到这个错误,因为我在 Python 方面没有经验。非常感谢您的帮助!

由于您使用的是 Python 多行字符串,因此您不需要包括换行符、额外的引号和缩进。请尝试以下操作:

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()

这将创建以下二维码:

您可以执行以下操作以避免需要额外的变量:

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()