vendredi 28 mai 2010

Generating badges for upcoming seminar ;)

#!/usr/bin/env python

import sys
from csv import reader
from mako.template import Template

def help(script_name):
    print "Usage: %s <csv-file> <badge-template>" % script_name

def write_page(template, num, person):
    f = open('badges%05d.svg' % num, 'w')
    f.write(template.render(**person))
    f.close()

def process_file(csv_file, template_file):
    rows = reader(open(csv_file, "rb"))
    template = Template(filename=template_file)
    person_cnt = 0
    pages_cnt = 1
    person = dict()
    for row in rows:
        person['name%d' % person_cnt] = row[0].strip()
        person['surname%d' % person_cnt] = row[1].strip()
        person['organization%d' % person_cnt] = row[2].strip()
        person_cnt += 1
        if person_cnt == 4:
            write_page(template, pages_cnt, person)
            person_cnt = 0
            pages_cnt += 1
    if person_cnt != 0:
        write_page(template, pages_cnt, person)

if __name__ == '__main__':
    if len(sys.argv) < 3:
        help()
    else:
        process_file(sys.argv[1], sys.argv[2])

Aucun commentaire:

Publier un commentaire