bmbf/generate_bmbf_list.py

101 lines
3.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2019-02-23 16:29:40 +01:00
import csv
import os
2022-05-15 18:51:06 +02:00
import sys
import tempfile
2019-02-23 16:29:40 +01:00
from operator import itemgetter
from fillpdf import fillpdfs
from pdfrw import PdfReader, PdfWriter
2019-02-23 16:29:40 +01:00
# Change to your needs
2022-10-26 03:47:23 +02:00
event_period = "28.10. - 01.11.22"
event_org = "Verein der Freunde und Förderer der Bundesfachschaftentagung Elektrotechnik e.V."
2022-10-26 03:47:23 +02:00
event_name = "91. Bundesfachschaftentagung Elektrotechnik"
attendees_csv = "teilnehmer_innen.csv"
empty_sheets = 1
2022-10-26 03:47:23 +02:00
template_form = "210706_Teilnehmendenliste_abgestimmt.pdf"
date_list = ["28.10.22", "29.10.22", "30.10.22", "31.10.22", "01.11.22"]
2019-02-23 16:29:40 +01:00
# Nothing to change below here
2019-02-23 16:29:40 +01:00
form_mapping = [("6", "20", "21"), ("19", "40", "41"), ("18", "39", "22"),
("17", "42", "23"), ("16", "43", "24"), ("15", "44", "25"), ("14", "45", "26"),
("13", "46", "27"), ("12", "Text7", "28"), ("11", "29", "34"), ("10", "30", "35"),
("9", "31", "36"), ("8", "32", "37"), ("7", "33", "38")]
2019-02-23 16:29:40 +01:00
def read_csv(csv_path):
try:
csvfile = open(csv_path, newline='')
except FileNotFoundError:
print("[X] File {} not found!".format(csv_path))
2022-05-15 18:51:06 +02:00
sys.exit(1)
else:
with csvfile:
2022-05-12 01:11:06 +02:00
reader = csv.DictReader(csvfile, delimiter=",")
persons = [{"Name": f"{t['Vorname']} {t['Nachname']}", "Hochschule": f"{t['Standort']}"} for t in reader if
t['Status'] == "Student"]
persons.sort(key=itemgetter("Hochschule", "Name"))
2022-05-15 18:51:06 +02:00
for idx, p in enumerate(persons):
p["Position"] = str(idx + 1)
2019-02-23 16:29:40 +01:00
return persons
2022-05-15 18:51:06 +02:00
def fill_template(template_path, persons_list, headers, output_path):
fields = {}
2019-02-23 16:29:40 +01:00
2022-05-15 18:51:06 +02:00
for person, mapping in zip(persons_list, form_mapping):
fields[mapping[0]] = person["Position"]
fields[mapping[1]] = person["Name"]
fields[mapping[2]] = person["Hochschule"]
2019-02-23 16:29:40 +01:00
2022-05-15 18:51:06 +02:00
fields = {**headers, **fields}
2019-02-23 16:29:40 +01:00
2022-05-15 18:51:06 +02:00
fillpdfs.write_fillable_pdf(input_pdf_path=template_path, output_pdf_path=output_path, data_dict=fields,
flatten=True)
2019-02-23 16:29:40 +01:00
2022-05-15 18:51:06 +02:00
def create_pdf(output_pdf, form, persons_chunks, zeitraum, form_date, org_name, name, add_sheets):
page = 1
output_file_name = "output"
2019-02-23 16:29:40 +01:00
writer = PdfWriter()
2022-05-15 18:51:06 +02:00
form_reader = PdfReader(template_form)
writer.trailer.Info = form_reader.Info
2019-02-23 16:29:40 +01:00
2022-05-15 18:51:06 +02:00
with tempfile.TemporaryDirectory() as workdir:
for persons_list in persons_chunks + [[] for i in range(add_sheets)]:
output_file_path = os.path.join(workdir, f'{output_file_name}{page}.pdf')
headers = {"1": page, "2": zeitraum, "3": form_date, "4": org_name, "5": name}
fill_template(template_path=form, persons_list=persons_list, headers=headers, output_path=output_file_path)
reader = PdfReader(output_file_path)
writer.addpages(reader.pages)
page += 1
writer.write(output_pdf)
2019-02-23 16:29:40 +01:00
if __name__ == "__main__":
cwd = os.getcwd()
csv_file_path = os.path.join(cwd, attendees_csv)
form_path = os.path.join(cwd, template_form)
# check if form exits
if not os.path.exists(form_path):
print("[X] File {} not found!".format(form_path))
2022-05-15 18:51:06 +02:00
sys.exit(1)
person_list = read_csv(csv_file_path)
persons_per_page = len(form_mapping)
person_list_per_page = [person_list[i:i + persons_per_page] for i in range(0, len(person_list), persons_per_page)]
for date in date_list:
2022-05-15 18:51:06 +02:00
final_pdf_name = f'teilnehmendenliste_{date.replace(".", "_").replace(" ", "")}.pdf'
final_pdf_path = os.path.join(cwd, final_pdf_name)
2022-05-15 18:51:06 +02:00
print(f'Creating {final_pdf_name}... ', end='')
create_pdf(final_pdf_path, form_path, person_list_per_page, event_period, date, event_org, event_name, empty_sheets)
print('done!')