-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
114 lines (87 loc) · 4.82 KB
/
generator.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
import os
import json
import sys
from fpdf import FPDF
from xhtml2pdf import pisa
def create_toimari_object(jaokset, toimarit, kuvat):
toimaritJaoksittain = {}
for jaos_nimi, jaos_virat in jaokset.items():
toimaritJaoksittain[jaos_nimi] = []
for toimari_nimi, toimari_virat in toimarit.items():
toimari_virat_jaoksessa = []
for virka in toimari_virat:
if virka in jaos_virat:
toimari_virat_jaoksessa.append(virka)
if len(toimari_virat_jaoksessa) > 0:
toimaritJaoksittain[jaos_nimi].append({
"nimi": toimari_nimi,
"virat": toimari_virat_jaoksessa,
"kuva": kuvat[toimari_nimi]
})
return toimaritJaoksittain
def create_html(data, default_image, cells_per_row, jaos_colors_and_order):
html_content = f'<div style="font-family: Source Sans Pro,Calibri,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji; padding: 0; width: 960px;">'
html_content += f'<div style="display: flex; flex-direction: row">'
counter = 0
for jaos in jaos_colors_and_order.keys():
toimihenkilot = sorted(data[jaos], key=lambda x: (x["virat"][0], x["nimi"]))
background_color = jaos_colors_and_order.get(jaos, "lightgray")
text_color = "black" if background_color == "#fbdb1d" else "white"
html_content += f'<div style="display: flex; text-align: left; justify-content: center; align-items: center; padding: 2% 1%; color: {text_color}; background-color: {background_color}; font-family: Lora,Helvetica,serif; font-style: italic; font-size: {18/cells_per_row*6}"><div style="width: {160/cells_per_row*6}"><h3>{jaos}</h3></div></div>'
if counter % cells_per_row == cells_per_row - 1:
html_content += '</div><div style="display: flex; flex-direction: row">'
counter += 1
for i, toimihenkilo in enumerate(toimihenkilot, start=1):
image_path = f"kuvat/{toimihenkilo['kuva']}"
# Check if the image file exists before adding the image tag
if os.path.exists(image_path):
img_tag = f'<div style="min-height: {250/cells_per_row*6}"><img src="{image_path}" width={160/cells_per_row*6} style="margin-bottom: 10px;"></div>'
else:
img_tag = f'<div style="min-height: {250/cells_per_row*6}"><img src="{default_image}" width={160/cells_per_row*6} style="margin-bottom: 10px;"></div>'
cell_html = f'<div style="display: inline-block; width: {160/cells_per_row*6}; text-align: center; padding: 3.5% 1%; color: {text_color}; background-color: {background_color}; font-size: {18/cells_per_row*6}">{img_tag}<br><b>{toimihenkilo["nimi"]}</b><br>{", ".join(toimihenkilo["virat"])}</div>'
html_content += cell_html
if counter % cells_per_row == cells_per_row - 1:
html_content += '</div><div style="display: flex; flex-direction: row">'
counter += 1
html_content += '</div>'
html_content += '</div>'
return html_content
# Read JSON files
with open('jaokset.json', encoding='utf-8') as f:
jaokset = json.load(f)
with open('toimarit.json', encoding='utf-8') as f:
toimarit = json.load(f)
with open('kuvat.json', encoding='utf-8') as f:
kuvat = json.load(f)
# Define background colors for each jaos
jaos_colors_and_order = {
"Fuksijaos": "#fbdb1d",
"Hyvinvointijaos": "#ff8a04",
"Infojaos": "#007bff",
"Opintojaos": "#28a745",
"Tapahtumajaos": "#911f2f",
"Ulkojaos": "#fbdb1d",
"Yrityssuhdejaos": "#ff8a04",
"Muut toimihenkilöt": "#007bff"
}
default_image = "kuvat/fii_pelle2-valk-1024x1024.png"
if __name__ == "__main__":
data = create_toimari_object(jaokset, toimarit, kuvat)
if len(sys.argv) != 2:
raise ValueError("Invalid number of arguments, expected 1 argument (cells per row)")
cells_per_row = sys.argv[1]
if not cells_per_row.isnumeric() or int(cells_per_row) < 1:
raise ValueError("Invalid number of cells per row")
html_content = create_html(data, default_image, int(cells_per_row), jaos_colors_and_order)
pdf_content = f'<html><head><style>img {{border-radius: 50%;}}@page {{size: a4 portrait}}</style></head><body style="margin: 0;">{html_content}</body></html>'
with open('toimihenkilot.html', 'w', encoding='utf-8') as html_file:
html_file.write(pdf_content)
#result_file = open("toimihenkilot.pdf", "w+b")
# convert HTML to PDF
#pisa_status = pisa.CreatePDF(
# pdf_content, # the HTML to convert
# dest=result_file) # file handle to recieve result
# close output file
#result_file.close() # close output file
# return False on success and True on errors
#print(pisa_status.err)