File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed
ch13-interact-with-pdf-files Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change 1
- # 13.2 - Concatenating and Merging PDFs
1
+ # 13.3 - Concatenating and Merging PDFs
2
2
# 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 )
You can’t perform that action at this time.
0 commit comments