-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf-merger.py
More file actions
51 lines (38 loc) · 1.18 KB
/
Copy pathpdf-merger.py
File metadata and controls
51 lines (38 loc) · 1.18 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
import os
import PyPDF2
import re
filepath = input('Enter 1st pdf path - ')
file1 = open(filepath, 'rb')
filepath = input('Enter 2nd pdf path - ')
file2 = open(filepath, 'rb')
reader1 = PyPDF2.PdfFileReader(file1)
reader2 = PyPDF2.PdfFileReader(file2)
writer = PyPDF2.PdfFileWriter()
print('First Pdf has ' + str(reader1.numPages) + ' pages')
print('Second Pdf has ' + str(reader2.numPages) + ' pages')
pageNos = []
print('Enter the order of pages in the format : "Pdf no - Starting Page - Ending Page" \nEnter -1 once you\'re done')
choiceRegex = re.compile(r'(\d) *- *(\d+) *- *(\d+)')
while True:
choice = input()
if choice == '-1':
break
mo = choiceRegex.findall(choice)
pageNos.append(mo[0])
for pages in pageNos:
pdf = int(pages[0])
start = int(pages[1])
end = int(pages[2])
if pdf == 1:
for pageNum in range(start-1, end):
page = reader1.getPage(pageNum)
writer.addPage(page)
else:
for pageNum in range(start-1, end):
page = reader2.getPage(pageNum)
writer.addPage(page)
outputFile = open('merged.pdf', 'wb')
writer.write(outputFile)
outputFile.close()
file1.close()
file2.close()