8000 Add solution to ch13 section 3 exercise 1 · tejussm/python-basics-exercises@97bf116 · GitHub
[go: up one dir, main page]

Skip to content

Commit 97bf116

Browse files
committed
Add solution to ch13 section 3 exercise 1
1 parent 822426a commit 97bf116

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
1-
# 13.2 - Concatenating and Merging PDFs
1+
# 13.3 - Concatenating and Merging PDFs
22
# Solutions to review exercises
3+
4+
# ***********
5+
# Exercise 1
6+
#
7+
# In the Chapter 13 Practice Files directory there are three PDFs called
8+
# `merge1.pdf`, `merge2.pdf`, and `merge3.pdf`. Using a `PdfFileMerger`
9+
# instance, concatenate the two files `merge1.pdf` and `merge2.pdf`
10+
# using`.append()`.
11+
# ***********
12+
13+
from pathlib import Path
14+
15+
from PyPDF2 import PdfFileMerger
16+
17+
18+
BASE_PATH = Path.home() / "github/realpython/python-basics-exercises/" \
19+
"ch13-interact-with-pdf-files/practice_files"
20+
21+
pdf_paths = [BASE_PATH / "merge1.pdf", BASE_PATH / "merge2.pdf"]
22+
pdf_merger = PdfFileMerger()
23+
24+
for path in pdf_paths:
25+
pdf_merger.append(str(path))
26+
27+
output_path = Path.home() / "concatenated.pdf"
28+
with output_path.open(mode="wb") as output_file:
29+
pdf_merger.write(output_file)

0 commit comments

Comments
 (0)
0