-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfmerger.py
204 lines (170 loc) · 5.36 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from PyPDF2 import PdfFileMerger
import os
import glob
from tkinter import filedialog
from tkinter import *
from tkinter import ttk
def browse_button1():
# Allow user to select a directory and store it in global var
# called folder_path
global folder_path
global first_folder
filename = filedialog.askdirectory()
first_folder = filename+'/'
print(first_folder)
if filename!='':
show_folder2_button()
def browse_button2():
# Allow user to select a directory and store it in global var
# called folder_path
global folder_path
global second_folder
filename = filedialog.askdirectory()
second_folder = filename+'/'
print(second_folder)
if filename!='':
show_res_btn()
def browse_res_btn():
# Allow user to select a directory and store it in global var
# called folder_path
global folder_path
global result_folder
filename = filedialog.askdirectory()
result_folder = filename+'/'
print(result_folder)
if filename!='':
show_merge_button()
def merge():
os.chdir(first_folder)
ctr=0
for pdf in glob.glob("*.pdf"):
ctr+=1
second_pdf=second_folder+os.path.splitext(pdf)[0]+'_A'+os.path.splitext(pdf)[1]
result = result_folder+os.path.splitext(pdf)[0]+'_M.pdf'
msg = '['+str(ctr)+'] Merged '+pdf+' and '+second_pdf+' to '+result
treev.insert("", 'end', text ="L1",
values =(ctr, pdf, os.path.splitext(pdf)[0]+'_A.pdf', os.path.splitext(pdf)[0]+'_M.pdf'))
pdf = first_folder + os.path.splitext(pdf)[0]+os.path.splitext(pdf)[1]
pdfs = [pdf,second_pdf]
merger = PdfFileMerger()
for pdf in pdfs:
merger.append(pdf)
merger.write(result)
merger.close()
mergeall()
def mergeall():
os.chdir(result_folder)
pdfs = [p for p in glob.glob("*.pdf")]
merger = PdfFileMerger()
for pdf in pdfs:
merger.append(pdf)
merger.write("result.pdf")
merger.close()
#================================================================#
window = Tk()
window.geometry("1152x700")
window.configure(bg = "#323232")
canvas = Canvas(
window,
bg = "#323232",
height = 700,
width = 1152,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
f = Frame(window)
f.place(x=40, y=166)
style = ttk.Style()
style.configure("Treeview",
background="#8a8a8a",
foreground="#000000",
rowheight=25,
fieldbackground="#8a8a8a")
style.configure("mystyle.Treeview", highlightthickness=0, bd=0, font=('Calibri', 11)) # Modify the font of the body
style.configure("mystyle.Treeview.Heading", font=('Calibri', 13,'bold')) # Modify the font of the headings
style.layout("mystyle.Treeview", [('mystyle.Treeview.treearea', {'sticky': 'nswe'})]) # Remove the borders
style.map('Treeview', background=[('selected', '#323232')])
scrollbar = Scrollbar(f)
# Using treeview widget
treev = ttk.Treeview(f, selectmode ='browse',height=15, style="mystyle.Treeview")
# Calling pack method w.r.to treeview
treev.place(x=40,y=166)
treev.pack()
# Constructing vertical scrollbar
# with treeview
treev.configure(xscrollcommand = scrollbar.set)
# Defining number of columns
treev["columns"] = ("1", "2", "3","4")
# Defining heading
treev['show'] = 'headings'
# Assigning the width and anchor to the
# respective columns
treev.column("1", width = 90, anchor ='c')
treev.column("2", width = 330, anchor ='c')
treev.column("3", width = 330, anchor ='c')
treev.column("4", width = 330, anchor ='c')
# Assigning the heading names to the
# respective columns
treev.heading("1", text ="Sr No")
treev.heading("2", text ="File 1")
treev.heading("3", text ="File 2")
treev.heading("4", text ="Result File")
img0 = PhotoImage(file = f"imgs/heading.png")
canvas.create_image(35,49, anchor=NW, image=img0)
# canvas.create_rectangle(
# 40, 166, 40+1074, 166+332,
# fill = "#8a8a8a",
# outline = "")
resultfol = PhotoImage(file = f"imgs/resultfol.png")
second = PhotoImage(file = f"imgs/second.png")
merge_img = PhotoImage(file = f"imgs/merge.png")
first = PhotoImage(file = f"imgs/first.png")
#=========================================================================#
def show_folder1_button():
b1 = Button(
image = first,
borderwidth = 0,
highlightthickness = 0,
command = browse_button1,
relief = "flat")
b1.place(
x = 40, y = 585,
width = 240,
height = 60)
def show_folder2_button():
b4 = Button(
image = second,
borderwidth = 0,
highlightthickness = 0,
command = browse_button2,
relief = "flat")
b4.place(
x = 310, y = 585,
width = 240,
height = 60)
def show_res_btn():
b3 = Button(
image = resultfol,
borderwidth = 0,
highlightthickness = 0,
command = browse_res_btn,
relief = "flat")
b3.place(
x = 580, y = 585,
width = 240,
height = 60)
def show_merge_button():
b2 = Button(
image = merge_img,
borderwidth = 0,
highlightthickness = 0,
command = merge,
relief = "flat")
b2.place(
x = 894, y = 585,
width = 220,
height = 60)
show_folder1_button()
window.resizable(False, False)
window.mainloop()