-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_QRcode_on_exam_single.py
180 lines (127 loc) · 4.83 KB
/
01_QRcode_on_exam_single.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# -*- coding: utf-8 -*-
"""
@author: ufriess
Adapted Robert Schmidt Jan-May 2022
INPUT:
pdf of exam
number of exams: NumKlausuren
"""
import pandas as pd
import numpy as np
import io
import os
from PyPDF2 import PdfFileWriter, PdfFileReader
import qrcode
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.utils import ImageReader
def AddKlausur(output, num, add_empty_page = False):
def deckblatt(can):
# enter exam number on front piece
can.setFontSize(12)
can.drawString(230, 530, '{:03d}'.format(num))
def fusszeile(can, num, aufgabe, seite):
# Text
can.setFontSize(10)
can.drawString(120, 25, 'Klausur Nr {:03d}, A{:02d}/{:02d}'.format(
num, aufgabe, seite)) # num=Nummer der Klausur
# QR code
qr = qrcode.QRCode(
# box_size=200,
# border=4,
)
# UID Gruppe Aufgabe Seite
qr.add_data('{:03d},{:02d},{:02d}'.format(
num, aufgabe, seite))
qr.make(fit=True)
qr = qr.make_image(fill_color="black", back_color="white")
io_img = io.BytesIO()
qr.save(io_img, 'PNG')
reportlab_io_img = ImageReader(io_img)
can.drawImage(reportlab_io_img, 30, 15, width = 60,
preserveAspectRatio = True,
anchor = 'sw')
# Horizontale Linie
can.setLineWidth(0.5)
can.line(30, 80, 560, 80)
can.setFontSize(10)
can.drawString(120, 40, 'Diesen Bereich bitte nicht beschriften')
a = 0
s = 1
outpagenum = 1
for pagenum, page in enumerate(Klausur.pages):
qrpage = io.BytesIO()
can = canvas.Canvas(qrpage, pagesize=A4)
if pagenum == 0:
# if data.Gruppe >0:
deckblatt(can)
if a==9 and s==1: # Zusätzblätter haben Seitenzahl >=3 um
s=s+2 # zugeordnet werden zu können
fusszeile(can, num, a, s)
can.save()
# move to the beginning of the StringIO buffer
qrpage.seek(0)
qr_pdf = PdfFileReader(qrpage)
# add QR code
# page.mergePage(qr_pdf.getPage(0))
# output.addPage(page)
qr_pdf.getPage(0).mergePage(page)
output.addPage(qr_pdf.getPage(0))
outpagenum += 1
if a <= NumAufgaben: # for regular problems increase count
a += 1
else: # for additional sheets raise page number by two
s +=2
# Erzeuge leere Seite - außer für Zusatzblätter
if a <= NumAufgaben:
if add_empty_page:
qrpage = io.BytesIO()
can = canvas.Canvas(qrpage, pagesize=A4)
fusszeile(can, a, s)
can.save()
# move to the beginning of the StringIO buffer
qrpage.seek(0)
qr_pdf = PdfFileReader(qrpage)
output.addPage(qr_pdf.getPage(0))
outpagenum +=1
s += 1
if (s > 2) and (a <= NumAufgaben):
a += 1
s = 1
#### Main Program
outpath = 'tray'
infile = 'klausur_demo.pdf'
# Nur eine einzige Datei mit allen Klausuren?
# Falls SingleFile = False, dann einzelne Ausgabedatei für jeden Raum.
# SingleFile = False empfohlen für Hauptklausur
SingleFile = False
# Ausgabedatei, falls SingleFile = True
outfile = 'Exams_PEP1_year.pdf'
# outfile = 'Nachklausuren_PEP1_WS1920_largefont.pdf'
# # Leere Klausuren
# n_leer = 0
add_empty_page = False
try:
os.makedirs(outpath)
except OSError:
pass
# Obere linke Koordinate für Teilnehmerinfo auf Deckblatt
deckblatt_coord = [300, 705]
# Zeilenabstand für Teilnehmerinfo auf Deckblatt
deckblatt_delta = 23
Klausur = PdfFileReader(open(infile, 'rb'))
NumAufgaben =8
NumKlausuren=10
# numbers avoided in planes and hotels
avoid_numbers=(13,14,17,113,114,117,213,214,217,313,314,317,413,414,417)
for i in range(NumKlausuren):
if i%30==0 and i>0:
print('{:02d}%'.format(int(100*i/NumKlausuren)))
if i+1 in avoid_numbers:
continue
output = PdfFileWriter()
AddKlausur(output, i+1, add_empty_page = add_empty_page)
# finally, write "output" to a real file
outputStream = open(os.path.join(outpath, os.path.basename(infile).split('.')[0] + '_{:03d}'.format(i+1) + '.pdf'), 'wb')
output.write(outputStream)
outputStream.close()