forked from ProfessorKazarinoff/combine-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine.py
45 lines (36 loc) · 1.09 KB
/
combine.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
# combine.py
"""
A Python script to combine pdf's. Uses PyPDF2.
$ pip install PyPDF2
change the file names as needed
$ python combine.py
"""
import sys
from PyPDF2 import PdfReader, PdfWriter
def merge_pdfs(paths, output):
# Create the writer object
pdf_writer = PdfWriter()
# Loop over the pages
for path in paths:
pdf_reader = PdfReader(path)
for page in range(len(pdf_reader.pages)):
# Add each page to the writer object
pdf_writer.add_page(pdf_reader.pages[page])
# Write out the merged PDF
with open(output, 'wb') as out:
pdf_writer.write(out)
if __name__ == '__main__':
# set output file path
output = 'merged.pdf'
# delete first arg which is executable itself
del sys.argv[0]
# print what's going to happen
print("Merging ", end='')
for arg in sys.argv:
print(arg, "", end='')
print("into", output)
# merge all files in argv via function call
try:
merge_pdfs(sys.argv, output)
except:
sys.exit("invalid filename(s)")