This repository was archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge-pdf
More file actions
executable file
·52 lines (40 loc) · 1.26 KB
/
merge-pdf
File metadata and controls
executable file
·52 lines (40 loc) · 1.26 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
#!/usr/bin/env python3
import subprocess
import sys
def print_help():
print("""
Merges mutliple PDF files into a single file.
### Usage
1. Ensure that you have `pip` installed.
2. Run the script with the files to be merged as arguments.
```
$ ./merge-pdf file1.pdf file2.pdf
```
3. The merged file is saved as `merged.pdf`.
""")
print("Installing requirements...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "PyPDF2"])
from PyPDF2 import PdfFileMerger
try:
if len(sys.argv) < 2:
print_help()
else:
# PDF files to be merged
selected_files = sys.argv[1:]
if selected_files == "":
print("\nError: No file selected!")
elif len(selected_files) == 1:
print("\nError: Only one file selected!")
else:
print("\nFiles to be merged:")
for f in selected_files:
print(f)
merger = PdfFileMerger()
for filename in selected_files:
merger.append(filename)
# save merged PDF
merger.write(open("merged.pdf", 'wb'))
print("\nPDF merge complete! Saved as: merged.pdf\n")
except Exception as e:
print(f"\nError: File could not be processed!\n{filename}")
print(e)