-
Notifications
You must be signed in to change notification settings - Fork 1
/
appendImage.py
630 lines (515 loc) · 25 KB
/
appendImage.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from reportlab.graphics import renderPDF
from PyPDF2 import PdfReader, PdfWriter
from svglib.svglib import svg2rlg
import os
import re
import time
import stat
def append_page(book_name, image_path):
pdf_filename = f"{book_name}.pdf"
page_width, page_height = A4
if os.path.exists(pdf_filename):
# print(f"'{pdf_filename}' exists. Appending the image.")
existing_pdf = PdfReader(pdf_filename)
output = PdfWriter()
for page_num in range(len(existing_pdf.pages)):
output.add_page(existing_pdf.pages[page_num])
packet = canvas.Canvas("temp_page.pdf", pagesize=A4)
packet.drawImage(image_path, 0, 0, width=page_width,
height=page_height)
packet.save()
new_page = PdfReader("temp_page.pdf")
output.add_page(new_page.pages[0])
with open(pdf_filename, "wb") as output_stream:
output.write(output_stream)
os.remove("temp_page.pdf")
else:
print(f"Creating '{pdf_filename}' and adding the image.")
c = canvas.Canvas(pdf_filename, pagesize=A4)
c.drawImage(image_path, 0, 0, width=page_width, height=page_height)
c.showPage()
c.save()
print(f"{image_path} successfully added.")
'''
def append_puzzle_page(pdf_file, svg_directory, background_image=None):
temp_pdf = "temp.pdf"
if os.path.exists(pdf_file):
print(f"Reading existing PDF: {pdf_file}")
with open(pdf_file, "rb") as f:
existing_pdf = PdfReader(f)
pdf_writer = PdfWriter()
for page_num, page in enumerate(existing_pdf.pages, start=1):
print(f"Adding existing page {page_num}")
pdf_writer.add_page(page)
with open(temp_pdf, "wb") as f_temp:
pdf_writer.write(f_temp)
print(f"Existing PDF pages saved to {temp_pdf}")
new_pdf = "new_pages.pdf"
c = canvas.Canvas(new_pdf, pagesize=A4)
width, height = A4
def extract_order_number(filename):
match = re.match(r"(\d+)", filename)
return int(match.group(1)) if match else float('inf')
for page_num, svg_filename in enumerate(sorted(os.listdir(svg_directory), key=extract_order_number), start=1):
if svg_filename.endswith("S.svg"):
continue
svg_filepath = os.path.join(svg_directory, svg_filename)
drawing = svg2rlg(svg_filepath)
if background_image:
c.drawImage(background_image, 0, 0, width=width, height=height)
drawing_width = drawing.width
drawing_height = drawing.height
scale_width = width / drawing_width
scale_height = height / drawing_height
scale = min(scale_width, scale_height)
drawing.scale(scale, scale)
renderPDF.draw(drawing, c, 0, 0)
print(f"Adding new page: {svg_filename}")
c.showPage()
c.save()
if os.path.exists(pdf_file):
print(f"Merging existing and new pages into {pdf_file}")
with open(temp_pdf, "rb") as f_existing, open(new_pdf, "rb") as f_new:
existing_pdf = PdfReader(f_existing)
new_pdf_content = PdfReader(f_new)
pdf_writer = PdfWriter()
with open(pdf_file, "wb") as f_final:
for page_num, page in enumerate(existing_pdf.pages, start=1):
print(f"Adding existing page {page_num}")
pdf_writer.add_page(page)
for page_num, page in enumerate(new_pdf_content.pages, start=1):
print(f"Adding new page {page_num}")
pdf_writer.add_page(page)
pdf_writer.write(f_final)
print(f"Final PDF saved as {pdf_file}")
f_existing.close()
f_new.close()
time.sleep(1)
def force_delete(file_path):
try:
os.remove(file_path)
print(f"Successfully deleted {file_path}")
except PermissionError:
print(f"PermissionError: Changing permissions for {file_path}")
os.chmod(file_path, stat.S_IWUSR)
os.remove(file_path)
print(f"Successfully deleted {
file_path} after changing permissions")
except Exception as e:
print(f"Error deleting {file_path}: {e}")
force_delete(temp_pdf)
force_delete(new_pdf)
# puzzle + solution pages
def append_puzzle_page(pdf_file, svg_directory, background_image=None):
temp_pdf = "temp.pdf"
solution_pdf = "solution_pages.pdf"
if os.path.exists(pdf_file):
print(f"Reading existing PDF: {pdf_file}")
with open(pdf_file, "rb") as f:
existing_pdf = PdfReader(f)
pdf_writer = PdfWriter()
for page_num, page in enumerate(existing_pdf.pages, start=1):
print(f"Adding existing page {page_num}")
pdf_writer.add_page(page)
with open(temp_pdf, "wb") as f_temp:
pdf_writer.write(f_temp)
print(f"Existing PDF pages saved to {temp_pdf}")
new_pdf = "new_pages.pdf"
new_solutions_pdf = "new_solutions.pdf"
c = canvas.Canvas(new_pdf, pagesize=A4)
c_solutions = canvas.Canvas(new_solutions_pdf, pagesize=A4)
width, height = A4
top_margin = 100
bottom_margin = 100
# Add "SOLUTIONS" cover page
solutions_cover = os.path.join(svg_directory, "S.svg")
if os.path.exists(solutions_cover):
if background_image:
c.drawImage(background_image, 0, 0, width=width, height=height)
cover_drawing = svg2rlg(solutions_cover)
cover_width = cover_drawing.width
cover_height = cover_drawing.height
scale_cover_width = width / cover_width
scale_cover_height = height / cover_height
scale_cover = min(scale_cover_width, scale_cover_height)
cover_drawing.scale(scale_cover, scale_cover)
renderPDF.draw(cover_drawing, c_solutions, 0, 0)
c_solutions.showPage()
print(f"Added SOLUTIONS cover page: {solutions_cover}")
else:
c_solutions.setFont("Helvetica-Bold", 48)
solutions_text = "SOLUTIONS"
c_solutions.drawString((width - c_solutions.stringWidth(solutions_text)) / 2, height / 2, solutions_text)
c_solutions.showPage()
def extract_order_number(filename):
match = re.match(r"(\d+)", filename)
return int(match.group(1)) if match else float('inf')
solutions_per_line = 2
max_lines_per_page = 3
y_position = height - top_margin - 150 # Starting position for the first line of solutions
solution_page_num = 0
for page_num, svg_filename in enumerate(sorted(os.listdir(svg_directory), key=extract_order_number), start=1):
if svg_filename.endswith("S.svg"):
continue
svg_filepath = os.path.join(svg_directory, svg_filename)
drawing = svg2rlg(svg_filepath)
if background_image:
c.drawImage(background_image, 0, 0, width=width, height=height)
drawing_width = drawing.width
drawing_height = drawing.height
scale_width = width / drawing_width
scale_height = height / drawing_height
scale = min(scale_width, scale_height)
drawing.scale(scale, scale)
renderPDF.draw(drawing, c, (width - drawing_width * scale) / 2, (height - drawing_height * scale) / 2)
print(f"Adding new page: {svg_filename}")
c.showPage()
# Add solution page
solution_filename = svg_filename.replace(".svg", "S.svg")
solution_filepath = os.path.join(svg_directory, solution_filename)
if os.path.exists(solution_filepath):
solution_drawing = svg2rlg(solution_filepath)
if solution_drawing:
solution_width = solution_drawing.width
solution_height = solution_drawing.height
scale_solution_width = (width / 2 - 50) / solution_width # Two solutions per line
scale_solution_height = (height - top_margin - bottom_margin) / (max_lines_per_page + 1) / solution_height # Adjust for multiple lines per page
scale_solution = min(scale_solution_width, scale_solution_height) * 1.2 # Increase size of the grids
if solution_height * scale_solution + y_position < bottom_margin:
c_solutions.showPage()
if background_image:
c_solutions.drawImage(background_image, 0, 0, width=width, height=height)
y_position = height - top_margin - 150
x_position = (width / 2 - solution_width * scale_solution) / 2 + (solution_page_num % solutions_per_line) * (width / 2) # Adjust x-position for two solutions per line
# Manually scale and position the drawing
c_solutions.saveState()
c_solutions.translate(x_position, y_position)
c_solutions.scale(scale_solution, scale_solution)
renderPDF.draw(solution_drawing, c_solutions, 0, 0)
c_solutions.restoreState()
# Add the name below each solution
c_solutions.setFont("Helvetica", 12)
solution_name = os.path.basename(solution_filepath).split(".")[1].replace("S", "")
text_width = c_solutions.stringWidth(solution_name, "Helvetica", 12)
c_solutions.drawString(x_position + (solution_width * scale_solution - text_width) / 2, y_position - 20, solution_name)
if solution_page_num % solutions_per_line == (solutions_per_line - 1):
y_position -= (solution_height * scale_solution + 100) # Adjust vertical position with proper spacing
if solution_page_num % (solutions_per_line * max_lines_per_page) == (solutions_per_line * max_lines_per_page - 1):
c_solutions.showPage()
if background_image:
c_solutions.drawImage(background_image, 0, 0, width=width, height=height)
y_position = height - top_margin - 150
solution_page_num += 1 # Increment solution page counter
print(f"Adding solution page: {solution_filename}")
c.save()
c_solutions.save()
# Merge puzzle and solution PDFs into the main PDF
if os.path.exists(pdf_file):
print(f"Merging existing and new pages into {pdf_file}")
with open(temp_pdf, "rb") as f_existing, open(new_pdf, "rb") as f_new, open(new_solutions_pdf, "rb") as f_solutions:
existing_pdf = PdfReader(f_existing)
new_pdf_content = PdfReader(f_new)
solutions_pdf_content = PdfReader(f_solutions)
pdf_writer = PdfWriter()
with open(pdf_file, "wb") as f_final:
for page_num, page in enumerate(existing_pdf.pages, start=1):
print(f"Adding existing page {page_num}")
pdf_writer.add_page(page)
for page_num, page in enumerate(new_pdf_content.pages, start=1):
print(f"Adding new page {page_num}")
pdf_writer.add_page(page)
for page_num, page in enumerate(solutions_pdf_content.pages, start=1):
print(f"Adding new solution page {page_num}")
pdf_writer.add_page(page)
pdf_writer.write(f_final)
print(f"Final PDF saved as {pdf_file}")
f_existing.close()
f_new.close()
f_solutions.close()
time.sleep(1)
def force_delete(file_path):
try:
os.remove(file_path)
print(f"Successfully deleted {file_path}")
except PermissionError:
print(f"PermissionError: Changing permissions for {file_path}")
os.chmod(file_path, stat.S_IWUSR)
os.remove(file_path)
print(f"Successfully deleted {file_path} after changing permissions")
except Exception as e:
print(f"Error deleting {file_path}: {e}")
force_delete(temp_pdf)
force_delete(new_pdf)
force_delete(new_solutions_pdf)
'''
def append_puzzle_page(pdf_file, svg_directory, background_image=None):
temp_pdf = "temp.pdf"
if os.path.exists(pdf_file):
print(f"Reading existing PDF: {pdf_file}")
with open(pdf_file, "rb") as f:
existing_pdf = PdfReader(f)
pdf_writer = PdfWriter()
for page_num, page in enumerate(existing_pdf.pages, start=1):
print(f"Adding existing page {page_num}")
pdf_writer.add_page(page)
with open(temp_pdf, "wb") as f_temp:
pdf_writer.write(f_temp)
print(f"Existing PDF pages saved to {temp_pdf}")
new_pdf = "new_pages.pdf"
new_solutions_pdf = "new_solutions.pdf"
c = canvas.Canvas(new_pdf, pagesize=A4)
c_solutions = canvas.Canvas(new_solutions_pdf, pagesize=A4)
width, height = A4
top_margin = 100
bottom_margin = 100
# Add "SOLUTIONS" cover page
if background_image:
c_solutions.drawImage(background_image, 0, 0,
width=width, height=height)
solutions_cover = os.path.join(svg_directory, "S.svg")
if os.path.exists(solutions_cover):
cover_drawing = svg2rlg(solutions_cover)
cover_width = cover_drawing.width
cover_height = cover_drawing.height
scale_cover_width = width / cover_width
scale_cover_height = height / cover_height
scale_cover = min(scale_cover_width, scale_cover_height)
cover_drawing.scale(scale_cover, scale_cover)
renderPDF.draw(cover_drawing, c_solutions, 0, 0)
c_solutions.showPage()
print(f"Added SOLUTIONS cover page: {solutions_cover}")
else:
c_solutions.setFont("Helvetica-Bold", 48)
solutions_text = "SOLUTIONS"
c_solutions.drawString(
(width - c_solutions.stringWidth(solutions_text)) / 2, height / 2, solutions_text)
c_solutions.showPage()
def extract_order_number(filename):
match = re.match(r"(\d+)", filename)
return int(match.group(1)) if match else float('inf')
solutions_per_line = 2
max_lines_per_page = 3
# Starting position for the first line of solutions
y_position = height - top_margin - 150
solution_page_num = 0
for page_num, svg_filename in enumerate(sorted(os.listdir(svg_directory), key=extract_order_number), start=1):
if svg_filename.endswith("S.svg"):
continue
svg_filepath = os.path.join(svg_directory, svg_filename)
drawing = svg2rlg(svg_filepath)
if background_image:
c.drawImage(background_image, 0, 0, width=width, height=height)
drawing_width = drawing.width
drawing_height = drawing.height
scale_width = width / drawing_width
scale_height = height / drawing_height
scale = min(scale_width, scale_height)
drawing.scale(scale, scale)
renderPDF.draw(drawing, c, (width - drawing_width * scale) /
2, (height - drawing_height * scale) / 2)
print(f"Adding new page: {svg_filename}")
c.showPage()
# Add solution page
solution_filename = svg_filename.replace(".svg", "S.svg")
solution_filepath = os.path.join(svg_directory, solution_filename)
if os.path.exists(solution_filepath):
solution_drawing = svg2rlg(solution_filepath)
if solution_page_num == 0 and background_image:
c_solutions.drawImage(
background_image, 0, 0, width=width, height=height)
if solution_drawing:
solution_width = solution_drawing.width
solution_height = solution_drawing.height
# Two solutions per line
scale_solution_width = (width / 2 - 50) / solution_width
scale_solution_height = (height - top_margin - bottom_margin) / (
max_lines_per_page + 1) / solution_height # Adjust for multiple lines per page
scale_solution = min(scale_solution_width,
scale_solution_height) * 1.2
if solution_height * scale_solution + y_position < bottom_margin:
c_solutions.showPage()
if background_image:
c_solutions.drawImage(
background_image, 0, 0, width=width, height=height)
y_position = height - top_margin - 150
x_position = (width / 2 - solution_width * scale_solution) / 2 + (solution_page_num %
# Adjust x-position for two solutions per line
solutions_per_line) * (width / 2)
# Manually scale and position the drawing
c_solutions.saveState()
c_solutions.translate(x_position, y_position)
c_solutions.scale(scale_solution, scale_solution)
renderPDF.draw(solution_drawing, c_solutions, 0, 0)
c_solutions.restoreState()
# Add the name below each solution
c_solutions.setFont("Helvetica", 12)
solution_name = os.path.basename(solution_filepath).split(".")[
1].replace("S", "")
text_width = c_solutions.stringWidth(
solution_name, "Helvetica", 12)
c_solutions.drawString(
x_position + (solution_width * scale_solution - text_width) / 2, y_position - 20, solution_name)
if solution_page_num % solutions_per_line == (solutions_per_line - 1):
y_position -= (solution_height * scale_solution + 50)
if solution_page_num % (solutions_per_line * max_lines_per_page) == (solutions_per_line * max_lines_per_page - 1):
c_solutions.showPage()
if background_image:
c_solutions.drawImage(
background_image, 0, 0, width=width, height=height)
y_position = height - top_margin - 150
solution_page_num += 1
print(f"Adding solution: {solution_filename}")
c.save()
c_solutions.save()
# Merge puzzle and solution PDFs into the main PDF
if os.path.exists(pdf_file):
print(f"Merging existing and new pages into {pdf_file}")
with open(temp_pdf, "rb") as f_existing, open(new_pdf, "rb") as f_new, open(new_solutions_pdf, "rb") as f_solutions:
existing_pdf = PdfReader(f_existing)
new_pdf_content = PdfReader(f_new)
solutions_pdf_content = PdfReader(f_solutions)
pdf_writer = PdfWriter()
with open(pdf_file, "wb") as f_final:
for page_num, page in enumerate(existing_pdf.pages, start=1):
print(f"Adding existing page: {page_num}")
pdf_writer.add_page(page)
for page_num, page in enumerate(new_pdf_content.pages, start=1):
print(f"Adding new page: {page_num}")
pdf_writer.add_page(page)
for page_num, page in enumerate(solutions_pdf_content.pages, start=1):
print(f"Adding new solution: {page_num}")
pdf_writer.add_page(page)
pdf_writer.write(f_final)
print(f"Final PDF saved as {pdf_file}")
f_existing.close()
f_new.close()
f_solutions.close()
time.sleep(1)
def force_delete(file_path):
try:
os.remove(file_path)
print(f"Successfully deleted {file_path}")
except PermissionError:
print(f"PermissionError: Changing permissions for {file_path}")
os.chmod(file_path, stat.S_IWUSR)
os.remove(file_path)
print(f"Successfully deleted {
file_path} after changing permissions")
except Exception as e:
print(f"Error deleting {file_path}: {e}")
force_delete(temp_pdf)
force_delete(new_pdf)
force_delete(new_solutions_pdf)
def append_solutions_page(pdf_file, svg_directory, background_image=None):
"""
Append solution pages to the existing PDF file with enhancements.
"""
temp_pdf = "temp_solutions.pdf"
if os.path.exists(pdf_file):
with open(pdf_file, "rb") as f:
existing_pdf = PdfReader(f)
pdf_writer = PdfWriter()
for page in existing_pdf.pages:
pdf_writer.add_page(page)
with open(temp_pdf, "wb") as f_temp:
pdf_writer.write(f_temp)
new_pdf = "solution_pages.pdf"
c = canvas.Canvas(new_pdf, pagesize=A4)
width, height = A4
margin = 50
usable_width = width - 2 * margin
max_svg_height = 250 # Increased SVG size for better visibility
def parse_solution_file(filename):
match = re.match(r"^\d+\.\s*(\d+)(N|H|BN|BH)(\d+)(S?)\.svg$", filename)
if match:
topic, mode, puzzle_number, solution_flag = match.groups()
return {
"topic": int(topic),
"mode": mode,
"puzzle_number": int(puzzle_number),
"is_solution": solution_flag == "S"
}
return None
solution_files = [f for f in os.listdir(
svg_directory) if f.endswith("S.svg")]
grouped_solutions = {}
for solution_file in solution_files:
details = parse_solution_file(solution_file)
if details:
topic = details["topic"]
mode = details["mode"]
if topic not in grouped_solutions:
grouped_solutions[topic] = {
"N": [], "H": [], "BN": [], "BH": []}
grouped_solutions[topic][mode].append(solution_file)
is_first_page = True
for topic, modes in sorted(grouped_solutions.items()):
if is_first_page:
c.setFont("Helvetica-Bold", 36)
c.drawCentredString(width / 2, height / 2, "SOLUTIONS")
is_first_page = False
c.showPage()
c.setFont("Helvetica-Bold", 20)
c.drawCentredString(width / 2, height - 50, f"Topic: {topic}")
y_offset = height - 100
for mode_name, mode_files in [("Normal", "N"), ("Hard", "H"), ("Bonus I", "BN"), ("Bonus II", "BH")]:
if mode_files not in modes or not modes[mode_files]:
continue
for i, solution_file in enumerate(sorted(modes[mode_files]), start=1):
svg_filepath = os.path.join(svg_directory, solution_file)
drawing = svg2rlg(svg_filepath)
# Scale SVG to fit better
drawing_width, drawing_height = drawing.width, drawing.height
scale = min((usable_width / 2) / drawing_width,
max_svg_height / drawing_height)
drawing.scale(scale, scale)
# Position SVG side-by-side
x_position = margin if i % 2 != 0 else margin + usable_width / 2
renderPDF.draw(drawing, c, x_position,
y_offset - drawing_height * scale)
# Add file name below SVG
c.setFont("Helvetica", 10)
file_name = os.path.basename(solution_file)
c.drawCentredString(x_position + (usable_width / 4),
y_offset - drawing_height * scale - 10, file_name)
if i % 2 == 0: # Move down after two per row
y_offset -= max_svg_height + 40
# Page overflow
if y_offset - max_svg_height < margin:
if background_image:
c.drawImage(background_image, 0, 0,
width=width, height=height)
c.showPage()
y_offset = height - 100
c.setFont("Helvetica-Bold", 20)
c.drawCentredString(
width / 2, height - 50, f"Topic: {topic}")
y_offset -= 40
c.save()
# Merge PDFs
if os.path.exists(pdf_file):
with open(temp_pdf, "rb") as f_existing, open(new_pdf, "rb") as f_new:
existing_pdf = PdfReader(f_existing)
new_pdf_content = PdfReader(f_new)
pdf_writer = PdfWriter()
with open(pdf_file, "wb") as f_final:
for page in existing_pdf.pages:
pdf_writer.add_page(page)
for page in new_pdf_content.pages:
pdf_writer.add_page(page)
pdf_writer.write(f_final)
os.remove(temp_pdf)
os.remove(new_pdf)
def main():
image_path = input("Enter the image path: ")
append_page("Where's Word-o", image_path)
if __name__ == '__main__':
# main()
# append_solutions_page("Test.pdf", "generated_puzzles", "Assets/Background.png")
append_puzzle_page(
"Test.pdf",
"generated_puzzles",
"Assets/pageBackground.png"
)