-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_vanha.py
executable file
·154 lines (122 loc) · 4.21 KB
/
print_vanha.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# coding=utf-8
from fpdf import FPDF
from itertools import zip_longest
import csv
import sys
from collections import namedtuple
import re
# C5 letter in millimeters
A4_X = 210
A4_Y = 294
#IMG_FILENAME = 'logo-mustavalkoinen-valkoiselle.png'
IMG_FILENAME = 'logo-varillinen.png'
FONT_FILENAME = 'fonts/Ubuntu-R.ttf'
TEXT = "Vetoamme Kokoomuksen puoluevaltuustoon, jotta se hyväksyisi turkistarhauksen kieltoa ajavan aloitteen."
PRINT_LOGO = True
FONT_SIZE = 7
DEBUG = 0
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(fillvalue=fillvalue, *args)
class PDF(FPDF):
def header(self):
self.image(
IMG_FILENAME,
0.03 * A4_X, # X
0.015 * A4_Y, # Y
0.20 * A4_X) # W
self.set_xy(0.40 * A4_X, 0.019 * A4_Y)
self.set_font_size(8)
self.multi_cell(
0.52 * A4_X, # X
0.01 * A4_Y, # Y
TEXT)
def footer(self):
self.set_y(-15)
self.cell(0, 10, '%s / {nb}' % self.page_no(), 0, 0, 'C')
def write_document(out, rows):
"""Create pdf from addresses and write it to output file."""
pdf = PDF('P', 'mm', 'A4')
pdf.alias_nb_pages()
pdf.add_font('ubuntu', '', FONT_FILENAME, uni=True)
pdf.set_font('ubuntu', '', FONT_SIZE)
# TODO Make offsets % - based
for group in grouper(255, rows):
pdf.add_page()
group1 = group[:85]
group2 = group[85:170]
group3 = group[170:]
string1 = '\n'.join([
(x.etunimi.title() + ' ' + x.sukunimi.title() +
((', ' + x.paikkakunta.title()) if x.paikkakunta else ''))
for x in group1 if x
])
string2 = '\n'.join([
(x.etunimi.title() + ' ' + x.sukunimi.title() +
((', ' + x.paikkakunta.title()) if x.paikkakunta else ''))
for x in group2 if x
])
string3 = '\n'.join([
(x.etunimi.title() + ' ' + x.sukunimi.title() +
((', ' + x.paikkakunta.title()) if x.paikkakunta else ''))
for x in group3 if x
])
pdf.set_font('ubuntu', '', FONT_SIZE)
pdf.set_xy(0.04 * A4_X, 0.075 * A4_Y)
pdf.multi_cell(
0.30 * A4_X, # Line width
0.01 * A4_Y, # Line height
string1,
DEBUG,
'L')
pdf.set_xy(0.34 * A4_X, 0.075 * A4_Y)
pdf.multi_cell(
0.30 * A4_X, # Line width
0.01 * A4_Y, # Line height
string2,
DEBUG,
'L')
pdf.set_xy(0.64 * A4_X, 0.075 * A4_Y)
pdf.multi_cell(
0.30 * A4_X, # Line width
0.01 * A4_Y, # Line height
string3,
DEBUG,
'L')
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)
CSVRow = namedtuple("CSVRow", [format_field_name(x) for x in next(reader)])
reader = csv.reader(infile, dialect)
for data in map(CSVRow._make, reader):
yield data
if __name__ == '__main__':
OUTFILE = 'Vetoomuksen_allekirjoittajat.pdf'
data = []
emailit = set()
for row in load_csv(sys.stdin):
if row.sahkoposti not in emailit:
if len(row.etunimi) < 2 or len(row.sukunimi) < 2:
continue
# if not re.search(
# r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)',
# row.sahkoposti):
# print(row.etunimi, row.sukunimi, row.sahkoposti)
# continue
data.append(row)
emailit.add(row.sahkoposti)
write_document(OUTFILE, data)
print(f'wrote {OUTFILE} ({len(data)} rows).')