Last active
August 29, 2015 14:02
-
-
Save madflow/c216d24fde1be740c6cd to your computer and use it in GitHub Desktop.
qrcodes from csv file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Generate qrcode images from a csv file | |
""" | |
import argparse | |
import csv | |
import qrcode | |
import os | |
import base64 | |
import sys | |
OUTPUT_DIR = os.path.join(os.path.dirname(__file__), 'output') | |
def process_csv(args): | |
content_column = int(args.content) if args.content else 0 | |
output_column = int(args.output) if args.output else 1 | |
with open(args.file, 'rb') as csvfile: | |
dialect = csv.Sniffer().sniff(csvfile.read(1024)) | |
csvfile.seek(0) | |
reader = csv.reader(csvfile, dialect) | |
next(reader) # skip header row | |
for row in reader: | |
content = row[content_column] | |
output = row[output_column] | |
qr = qrcode.QRCode() | |
qr.add_data(content) | |
qr.make(fit=True) | |
img = qr.make_image() | |
target_file = os.path.join(OUTPUT_DIR, output + '.png') | |
img.save(target_file) | |
with open(target_file, "rb") as image_file: | |
encoded_string = base64.b64encode(image_file.read()) | |
parser = argparse.ArgumentParser(description='Generate qrcode images from a csv file') | |
parser.add_argument('file', help='The csv file') | |
parser.add_argument('--content', help='The column with the qrcode content') | |
parser.add_argument('--output', help='The column with the target filename (without extension)') | |
parser.set_defaults(func=process_csv) | |
args = parser.parse_args() | |
args.func(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
argparse==1.2.1 | |
qrcode==4.0.4 | |
wsgiref==0.1.2 | |
zbar==0.10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment