Last active
September 20, 2019 19:48
-
-
Save hyrsky/39666ab7672f3df14cc4fd880d1d8ada to your computer and use it in GitHub Desktop.
Generate labels from D8 Webforms 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 python3 | |
# Version 3 | |
from fpdf import FPDF | |
from collections import namedtuple | |
import itertools as IT | |
import argparse | |
import csv | |
import sys | |
parser = argparse.ArgumentParser(description=( | |
'Luo lomakkeen tuloksista label paperille tulostettavan pdf tiedoston.')) | |
parser.add_argument('--skip', | |
metavar='N', | |
type=int, | |
default=0, | |
help='Jätä tyhjäksi N labelia ensimmäiseltä sivulta') | |
parser.add_argument('input', | |
type=argparse.FileType('r'), | |
default=sys.stdin, | |
help='Lomakkeen tulokset -tiedosto') | |
parser.add_argument('--output', | |
metavar='out.pdf', | |
default='out.pdf', | |
help='Aseta pdf tiedoston polku') | |
parser.add_argument('--font-size', | |
metavar='12', | |
type=int, | |
default='12', | |
help='Aseta fontin koko') | |
parser.add_argument('--font', | |
metavar='arial.ttf', | |
default='./arial.ttf', | |
help='Aseta ttf fonttitiedosto') | |
parser.add_argument('--margin', | |
metavar='5 [mm]', | |
type=int, | |
default=5, | |
help='Aseta marginaali millimetreissä') | |
args = parser.parse_args() | |
FONT_FILENAME = args.font | |
FONT_SIZE = args.font_size | |
def grouper(n, iterable): | |
""" | |
>>> list(grouper(3, 'ABCDEFG')) | |
[['A', 'B', 'C'], ['D', 'E', 'F'], ['G']] | |
""" | |
iterable = iter(iterable) | |
return iter(lambda: list(IT.islice(iterable, n)), []) | |
def write_labels(out, to_addrs): | |
pdf = FPDF('P', 'mm', 'A4') | |
pdf.set_auto_page_break(False, 0.0) | |
pdf.add_font('arial', '', FONT_FILENAME, uni=True) | |
pdf.set_font('arial', '', FONT_SIZE) | |
pdf.add_page() | |
# Units in mm | |
W = 70 | |
H = 37 | |
MARGIN_X = args.margin | |
MARGIN_Y = args.margin | |
location = [args.skip % 3, min(args.skip, 23) // 3] | |
pdf.set_xy(location[0] * W + MARGIN_X, location[1] * H + MARGIN_Y) | |
for to_addr in to_addrs: | |
to_addr = "{fname} {lname}\n{address}\n{postal_code} {city}".format( | |
fname=to_addr.etunimi.strip().title(), | |
lname=to_addr.sukunimi.strip().title(), | |
address=to_addr.osoite.strip().title(), | |
postal_code=to_addr.postinumero.strip(), | |
city=to_addr.paikkakunta.strip().title()) | |
pdf.multi_cell(W, 7, to_addr, 0, 'L') | |
location[0] += 1 | |
if location[0] > 2: | |
location[0] = 0 | |
location[1] += 1 | |
if location[1] > 7: | |
location[1] = 0 | |
pdf.add_page() | |
pdf.set_xy(location[0] * W + MARGIN_X, location[1] * H + MARGIN_Y) | |
pdf.output(out, 'F') | |
def format_field_name(field_name): | |
field_name = field_name.lower().strip() | |
field_name = field_name.replace(' ', '_') | |
field_name = field_name.replace('-', '_') | |
field_name = field_name.replace('ä', 'a') | |
field_name = field_name.replace('ö', 'o') | |
field_name = field_name.replace(':', '') | |
field_name = field_name.replace('.', '') | |
return field_name.strip() | |
def load_csv(infile): | |
"""Return generator which outputs csv rows.""" | |
header = infile.readline() | |
dialect = csv.Sniffer().sniff(header) | |
reader = csv.reader((header, ), dialect) | |
Data = namedtuple("Data", [format_field_name(x) for x in next(reader)]) | |
reader = csv.reader(infile, dialect) | |
for data in map(Data._make, reader): | |
if data.haluan_saada_tietopaketin_perinteisessa_postissa: | |
yield data | |
if __name__ == '__main__': | |
INFILE = args.input | |
OUTFILE = args.output | |
write_labels(OUTFILE, load_csv(INFILE)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment