【问题标题】:How can I generate multiple Vcard QRCodes from a CSV-File in Python?如何从 Python 中的 CSV 文件生成多个 Vcard QRCode?
【发布时间】:2021-11-05 12:59:01
【问题描述】:
> 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 二维码。遗憾的是,扫描二维码时只检索到 URL。

很遗憾,我不认识这个错误,因为我对 Python 没有经验。非常感谢您的帮助!

【问题讨论】:

    标签: python csv qr-code vcf-vcard


    【解决方案1】:

    当您使用 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()
    

    【讨论】:

      猜你喜欢
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      相关资源
      最近更新 更多