forked from DedSecInside/Awesome-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine_pdfs.py
30 lines (23 loc) · 853 Bytes
/
combine_pdfs.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
import PyPDF2, os
#get all pdf files from directory
pdf_files=[filename for filename in os.listdir('.') if filename.endswith('.pdf')]
#sort filenames
pdf_files.sort()
count_merged_files = 0
pdfWriter = PyPDF2.PdfFileWriter()
#copy each pdf file to the final output file
for filename in pdf_files:
print(f'Merging \'{filename}\' to main pdf file.')
count_merged_files+=1
pdfFileObj = open(filename,'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj,strict=False)
#if you want to skip first page change the range below from 0 to 1
for page_num in range(0,pdfReader.numPages):
pageObj = pdfReader.getPage(page_num)
pdfWriter.addPage(pageObj)
#save output file
pdfOutput = open('combined.pdf','wb')
pdfWriter.write(pdfOutput)
pdfOutput.close()
print('=============================')
print(f'[+] {count_merged_files} files merged.')