Skip to content

Commit 414fc48

Browse files
committed
Add solution to ch13 section 3 exercise 2
1 parent 97bf116 commit 414fc48

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

ch13-interact-with-pdf-files/3-concatenating-and-merging-pdfs.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,26 @@
2727
output_path = Path.home() / "concatenated.pdf"
2828
with output_path.open(mode="wb") as output_file:
2929
pdf_merger.write(output_file)
30+
31+
32+
# ***********
33+
# Exercise 2
34+
#
35+
# Using the same `PdfFileMerger` instance from exercise 1, merge the
36+
# file `merge3.pdf` in-between the pages from `merge1.pdf` and
37+
# `merge2.pdf` using `.merge()`.
38+
#
39+
# The final result should be a PDF with three pages. The first page
40+
# should have the number `1` on it, the second should have `2`, and the
41+
# third should have `3`.
42+
# ***********
43+
44+
pdf_merger = PdfFileMerger()
45+
pdf_merger.append(str(output_path))
46+
47+
pdf_path = BASE_PATH / "merge3.pdf"
48+
pdf_merger.merge(1, str(pdf_path))
49+
50+
output_path = Path.home() / "merged.pdf"
51+
with output_path.open(mode="wb") as output_file:
52+
pdf_merger.write(output_file)

0 commit comments

Comments
 (0)