-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
111 lines (88 loc) · 3.31 KB
/
main.py
File metadata and controls
111 lines (88 loc) · 3.31 KB
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
from fpdf import FPDF
import os
import math
from PIL import Image
"""
A python code that prepares a test quiz with 4 questions on a page.
Author: https://github.com/semihV23
"""
print("Working directory:", os.getcwd())
with open("Header.txt", "r", encoding="utf-8") as f:
header = f.read().split("[[NewLine]]")
fileName = header[0].lower().replace("\n", "_").replace(
" ", "_") + "_" + header[1].strip("\n") + ".pdf"
images = [f for f in os.listdir("questions")
if os.path.isfile("questions/"+f) and f.lower().endswith((".jpg", ".png"))]
images.sort()
print("Found Images:", images)
pdf = FPDF()
pdf.add_page()
pdf.set_text_shaping(True)
pdf.add_font("Poppins", style="", fname="./fonts/Poppins/Poppins-Regular.ttf")
pdf.add_font("Poppins", style="B", fname="./fonts/Poppins/Poppins-Bold.ttf")
pdf.add_font("Poppins", style="I", fname="./fonts/Poppins/Poppins-Italic.ttf")
pdf.add_font("Poppins", style="bi",
fname="./fonts/Poppins/Poppins-BoldItalic.ttf")
pdf.add_font("DejaVu", style="", fname="./fonts/DejaVu/DejaVuSansCondensed.ttf")
pdf.add_font("DejaVu", style="B",
fname="./fonts/DejaVu/DejaVuSansCondensed-Bold.ttf")
pdf.add_font("DejaVu", style="I",
fname="./fonts/DejaVu/DejaVuSansCondensed-Bold.ttf")
pdf.add_font("DejaVu", style="bi",
fname="./fonts/DejaVu/DejaVuSansCondensed-BoldOblique.ttf")
defaultFontFamily = "Poppins"
pdf.set_font(family=defaultFontFamily, style="B", size=42)
pdf.write(text=header[0])
pdf.set_font(family=defaultFontFamily, size=15)
pdf.write(text=header[1]+"\n")
pdf.set_font(family=defaultFontFamily, size=15)
pdf.write(text=f'Soru sayısı: {len(images)}\n')
pdf.write(text=f'Sayfa sayısı: {math.ceil(len(images)/4)}\n')
pdf.set_font(family=defaultFontFamily, size=18)
pdf.write(text=header[2])
pdf.add_page()
pdf.set_line_width(0.3)
pdf.set_draw_color(r=0, g=0, b=0)
pdf.line(x1=105, y1=10, x2=105, y2=287)
for index, image in enumerate(images):
q_width = 90.0
q_height = 138.5
i_width, i_height = Image.open("./questions/"+image).size
if i_width/i_height > q_width/q_height:
q_height = q_width*(i_height/i_width)
match index % 4:
case 0:
pdf.image(
"./questions/"+image,
x=10, y=10,
w=q_width, h=q_height,
keep_aspect_ratio=True
)
case 1:
pdf.image(
"./questions/"+image,
x=10, y=148.5,
w=q_width, h=q_height,
keep_aspect_ratio=True
)
case 2:
pdf.image(
"./questions/"+image,
x=110, y=10,
w=q_width, h=q_height,
keep_aspect_ratio=True
)
case 3:
pdf.image(
"./questions/"+image,
x=110, y=148.5,
w=q_width, h=q_height,
keep_aspect_ratio=True
)
if index+1 != len(images):
pdf.add_page()
pdf.set_line_width(0.3)
pdf.set_draw_color(r=0, g=0, b=0)
pdf.line(x1=105, y1=10, x2=105, y2=287)
pdf.output("./PDFs/"+fileName)
print("Done. File saved as:", fileName)