-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdfmerger.py
121 lines (86 loc) · 2.66 KB
/
pdfmerger.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
from glob import glob
import PyPDF2
from PyPDF2 import PdfFileMerger
import function as f
import output as out
VERSION = "v1.0"
out.header(VERSION)
start = True
while True:
if start:
out.head("INPUT")
while True:
SOURCE = input(
"│ Path to PDFs > ")
if "\\" not in SOURCE:
continue
OUT_NAME = input("│ PDF output name > ")
break
else:
out.head("Change directory? (y/n)")
if out.yninput("Input"):
while True:
SOURCE = input("│ Path to PDFs > ") # input("Pfad angeben > ")
if "\\" not in SOURCE and "/" not in SOURCE:
print("│ [INFO] Need a Path")
continue
OUT_NAME = input("│ PDF output name > ")
break
if SOURCE[len(SOURCE) - 1] != "\\":
SOURCE += "\\"
if ".pdf" not in OUT_NAME:
OUT_NAME += ".pdf"
pdf_list = []
for i, item in enumerate(os.listdir(SOURCE)):
if item.endswith('.pdf'):
pdf_list.append(item)
if len(pdf_list) == 0:
print("│ [INFO] No PDFs in directory found")
out.endblock()
continue
out.endblock()
out.head("Operation needed (order, exclude)? (y/n)")
operation = out.yninput("Input")
out.endblock()
if operation:
first = True
while True:
if first:
out.head("Change (c) - Exclude (e) - Continue (x)")
first = False
op = input("│ Input > ")
if op.upper() == "C":
out.endblock()
pdf_list = f.changelist(pdf_list)
first = True
continue
if op.upper() == "E":
out.endblock()
pdf_list = f.editlist(pdf_list)
first = True
continue
if op.upper() == "X":
out.endblock()
break
out.head("Order")
out.showlist(pdf_list)
out.endblock()
while True:
out.head("Start merging? (y/n)")
if not out.yninput("Execute"):
start = False
out.endblock()
break
else:
out.endblock()
merger = PyPDF2.PdfWriter()
for item in pdf_list:
tmp_pdf = PyPDF2.PdfFileReader(SOURCE + item)
for page in tmp_pdf.pages:
merger.addPage(page=page)
merger.write(open(SOURCE + OUT_NAME, 'wb'))
print(" PDF CREATED ".center(70, "—"))
print("\n\n")
start = False
break