bmbf/generate_bmbf_list.py

104 lines
3.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2019-02-23 16:29:40 +01:00
from fdfgen import forge_fdf
import sh
from math import ceil
import csv
from operator import itemgetter
from pathlib import Path
2019-02-23 16:29:40 +01:00
######## Bitte hier Anpassen ##########################################
2022-05-12 01:11:06 +02:00
maßnahmenzeitraum = "25.05. - 29.05.22"
datum="25.05.22"
kif_ev= "Verein der Freunde und Förderer der Bundesfachschaftentagung Elektrotechnik e.V."
maßname="90. Bundesfachschaftentagung Elektrotechnik"
csv_file_name="teilnehmer_innen.csv"
2022-05-12 01:11:06 +02:00
leer_blaetter=1
vorlage = "210705_Teilnehmendenliste_abgestimmt.pdf"
datum_list = ["25.05.22","26.05.22","27.05.22","28.05.22","29.05.22",]
2019-02-23 16:29:40 +01:00
######## Nothing to change below here ##########################################
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")]
def readcsv(csv_file_name):
persons = []
try:
csvfile = open(csv_file_name,newline='')
except FileNotFoundError:
print("[X] File {} not found!".format(csv_file_name))
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"))
2019-02-23 16:29:40 +01:00
return persons
def generate_pdfs(persons, maßnahmenzeitraum, datum, kif_ev, maßname, leer_blaetter):
seite = 1
data_file="data_fdf.bin"
output_file_name="output"
personen_pro_seite = 14
#check if vorlage exits
test_file = Path(vorlage)
if not test_file.is_file():
print("[X] File {} not found!".format(vorlage))
exit(-1)
2019-02-23 16:29:40 +01:00
anzahl_seiten = ceil(len(persons)/personen_pro_seite)
pdfs = []
for j in range(anzahl_seiten+leer_blaetter):
felder = []
i=0
for item in form_mapping:
if personen_pro_seite*j+i < len(persons):
felder.append((item[0],str(j*personen_pro_seite+i+1)))
felder.append((item[1], persons[j*personen_pro_seite+i]["Name"]))
felder.append((item[2], persons[j*personen_pro_seite+i]["Hochschule"]))
2019-02-23 16:29:40 +01:00
i+=1
if i == personen_pro_seite:
output_file = output_file_name + str(seite) +".pdf"
headers = [("1", seite), ("2", maßnahmenzeitraum), ("3", datum), ("4", kif_ev), ("5", maßname)]
fields = felder + headers
fdf = forge_fdf("",fields,[],[],[])
fdf_file = open(data_file,"wb")
fdf_file.write(fdf)
fdf_file.close()
sh.pdftk(vorlage,"fill_form", data_file,"output",output_file,"flatten")
2019-02-23 16:29:40 +01:00
pdfs.append(output_file)
seite+=1
return pdfs
if __name__ == "__main__":
person_list = readcsv(csv_file_name)
for datum in datum_list:
final_pdf_name = "teilnehmendenliste_{}.pdf".format(datum.replace(". ", "_"))
2019-02-23 16:29:40 +01:00
output_files = generate_pdfs(person_list, maßnahmenzeitraum, datum, kif_ev, maßname, leer_blaetter)
sh.pdftk(output_files, "cat","output",final_pdf_name )