-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
382 lines (312 loc) · 12.8 KB
/
Copy pathmain.py
File metadata and controls
382 lines (312 loc) · 12.8 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from tkinter import *
from tkinter import filedialog
import concurrent.futures
from tkinter import messagebox
# COMPARE WORD FILE PROGRAMME
import difflib
import docx2txt
total_point = 100.0
main_file_path = None
compare_file_path = None
accuracy_holder = None
cases = []
comma_count = 0
dot_count = 0
space_count = 0
inverted_comma_count = 0
double_inverted_comma_count = 0
exclamation_count = 0
other_count = 0
spelling_capital_small = 0
from reportlab.platypus import SimpleDocTemplate, Image, Table, TableStyle, Spacer, Paragraph, PageBreak
from reportlab.lib.styles import ParagraphStyle, TA_CENTER
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
def create_pdf_reportlab():
logo = "./img.png"
total_count = comma_count + dot_count + space_count + inverted_comma_count + double_inverted_comma_count \
+ exclamation_count + other_count + spelling_capital_small
total_error = float(comma_count * 0.50) + float(dot_count * 0.50) + float(space_count * 0.75) \
+ float(inverted_comma_count * 0.50) + float(double_inverted_comma_count * 0.50) \
+ float(exclamation_count * 0.50) + float(other_count * 0.50) + float(spelling_capital_small * 0.75)
data = [
["No.", "Description", "Total Mistakes", "Per Mistake Count", "Total"],
["1", "Comma", str(comma_count), "0.50", str(float(comma_count * 0.50))],
["2", "Dot", str(dot_count), "0.50",str(float(dot_count * 0.50))],
["3", "Space", str(space_count), "0.75",str(float(space_count * 0.75))],
["4", "Inverted Comma", str(inverted_comma_count), "0.50",str(float(inverted_comma_count * 0.50))],
["5", "Double Inverted Comma",str(double_inverted_comma_count), "0.50", str(float(double_inverted_comma_count * 0.50))],
["6", "Exclamation",str(exclamation_count), "0.50",str(float(exclamation_count * 0.50))],
# ["7", "Other",str(other_count), "0.50",str(float(other_count * 0.50))],
["7", "Spelling Mistake",str(spelling_capital_small), "0.75", str(float(spelling_capital_small * 0.75))],
["", "Total", str(total_count), "2.5", str(total_error)]
]
files_extentions = [('pdf', '*.pdf*')]
file_location = filedialog.asksaveasfile(filetypes = files_extentions, defaultextension = files_extentions)
if file_location == None or file_location == '':
return
pdf = SimpleDocTemplate(
filename=file_location.name,
pagesize=letter,
topMargin=10
)
table = Table(data)
style = TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.black),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Courier-Bold'),
('FONTNAME', (0, 0), (-1, 0), 'Courier-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 14),
('FONTSIZE', (0, 4), (-1, 0), 14),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('TOPPADDING', (0, 0), (-1, 0), 12),
('GRID', (0, 0), (-1, -1), 0.25, colors.black)
])
para_style = ParagraphStyle(
name='bold',
fontSize=30,
alignment=TA_CENTER
)
footer_style = ParagraphStyle(
name='bold',
fontSize=12,
alignment=TA_CENTER
)
table.setStyle(style)
final_pdf = []
im = Image(logo)
accuracy_text_pdf = Paragraph("Your Accuracy is "+str(total_point)+"%", para_style)
space = Spacer(width=0, height=40)
final_pdf.append(im)
final_pdf.append(space)
final_pdf.append(Paragraph("Typing Test Result", para_style))
final_pdf.append(Spacer(width=0, height=60))
final_pdf.append(table)
final_pdf.append(Spacer(width=0, height=40))
final_pdf.append(accuracy_text_pdf)
final_pdf.append(Spacer(width=0, height=240))
final_pdf.append(Paragraph("For further information contact \n https://github.com/codeterrayt - rohanprajapati7860@gmail.com", footer_style))
pdf.build(final_pdf)
messagebox.showinfo("showinfo", "Pdf Generated Successfully!")
# _ignore = [".",",","-","_"]
#
# def get_word(index):
# main_file_sentence = cases[0][0]
# back_count = index
# add_count = index
#
# check_first = False
# check_second = False
#
# for check_space in range(0,100):
# try:
# if main_file_sentence[back_count] == " " or main_file_sentence[back_count] == "" or main_file_sentence[back_count] in _ignore:
# if main_file_sentence[back_count] == " ":
# back_count +=1
# else:
# check_first = True
# else:
# back_count -= 1
# except:
# pass
# try:
# if main_file_sentence[add_count] == " " or main_file_sentence[add_count] == "" or main_file_sentence[
# add_count] in _ignore:
# check_second = True
# else:
# add_count +=1
# except:
# pass
#
# if check_first and check_second:
# break
#
# if back_count < 0:
# back_count = 0
# return [back_count,add_count]
# print(main_file_sentence[back_count:add_count])
# print([back_count, add_count])
def depth_compare(s,i):
global total_point
global dot_count
global comma_count
global space_count
global inverted_comma_count
global double_inverted_comma_count
global exclamation_count
global other_count
global spelling_capital_small
single_inverted_comma = ["\u0027", "\u0060", "\u00B4", "\u2018", "\u2019"]
double_invered_comma = ["\u02EE", "\u0022", "\u3003", "\u05F2", "\u1CD3", "\u02f6", "\u201D", "\u201F", "\u201C"]
if s[0] == ' ':
pass
elif s[0] == '-':
if s[-1] in single_inverted_comma or s[-1] in double_invered_comma or s[-1] == "." or s[-1] == "," or s[-1] == "'" or s[-1] == '"' or s[-1] == '“' or s[-1] == '‘' or s[-1] == "!":
if s[-1] == ".":
dot_count +=1
if s[-1] == ",":
comma_count +=1
if s[-1] in single_inverted_comma:
inverted_comma_count +=1
if s[-1] in double_invered_comma:
double_inverted_comma_count +=1
if s[-1] == "!":
exclamation_count +=1
total_point = total_point - 0.50
elif s[-1] == " ":
space_count +=1
total_point = total_point - 0.75
else:
spelling_capital_small +=1
total_point = total_point - 0.75
# print(u'not Present "{}" from position {}'.format(s[-1], i))
elif s[0] == '+':
if s[-1] in single_inverted_comma or s[-1] in double_invered_comma or s[-1] == "." or s[-1] == "," or s[
-1] == "'" or s[-1] == '"' or s[-1] == '“' or s[-1] == '‘' or s[-1] == "!":
if s[-1] == ".":
dot_count += 1
if s[-1] == ",":
comma_count += 1
if s[-1] in single_inverted_comma:
inverted_comma_count += 1
if s[-1] in double_invered_comma:
double_inverted_comma_count += 1
if s[-1] == "!":
exclamation_count +=1
total_point = total_point - 0.50
elif s[-1] == " ":
space_count +=1
total_point = total_point - 0.75
else:
spelling_capital_small +=1
total_point = total_point - 0.75
# spelling_capital_small +=1
# total_point = total_point - 0.75
# myWord = get_word(i)
# print(cases[0][1][myWord[0]:myWord[1]]+" => "+cases[0][0][myWord[0]:myWord[1]] )
# print(u'Extra "{}" to position {}'.format(s[-1], i))
def compare():
global cases
main_doc = ''
compare_doc = ''
main_file_path_list = list(main_file_path)
compare_file_path_list = list(compare_file_path)
try:
main_file_path_list.sort(key=lambda f: int(re.sub('\D', '', f)))
compare_file_path_list.sort(key=lambda f: int(re.sub('\D', '', f)))
except:
pass
for docx_file in main_file_path_list:
main_doc += docx2txt.process(docx_file)
for compare_docx_File in compare_file_path_list:
compare_doc += docx2txt.process(compare_docx_File)
cases = [(main_doc, compare_doc)]
for a, b in cases:
for i, s in enumerate(difflib.ndiff(a, b)):
depth_compare(s,i)
return total_point
def generate_pdf():
if accuracy_holder != None:
create_pdf_reportlab()
else:
messagebox.showinfo("showinfo", "Please First Compare The File")
# GUI
root = Tk()
root.title("Compare Docx")
root.minsize(500,500)
root.maxsize(500,500)
root.iconphoto(False,PhotoImage(file="./myicon.png"))
def reset_all():
global total_point
global main_file_path
global compare_file_path
global accuracy_holder
global dot_count
global comma_count
global space_count
global cases
global inverted_comma_count
global double_inverted_comma_count
global exclamation_count
global other_count
global spelling_capital_small
total_point = 100.0
main_file_path = None
compare_file_path = None
accuracy_holder = None
dot_count = 0
comma_count = 0
space_count = 0
inverted_comma_count = 0
double_inverted_comma_count = 0
exclamation_count = 0
other_count = 0
spelling_capital_small = 0
cases = []
generate_pdf_btn.pack_forget()
accuracy.config(text="")
reset_btn.pack_forget()
main_path_label.config(text="File Not Selected" , fg="red")
compare_path_label.config(text="File Not Selected" , fg="red")
choose_main_file_btn.config(text="Choose", bg="black" )
choose_compare_file_btn.config(text="Choose", bg="black")
start_comparision_btn.config(text="Start Comparison", bg="black")
def chooseMainFile():
global main_file_path
global accuracy_holder
accuracy_holder = None
main_file_path = filedialog.askopenfilenames(filetypes=[("Excel files", ".docx .doc")])
if main_file_path != '':
main_path_label.config(text="File Selected!" , fg="green")
choose_main_file_btn.config(bg="blue" , text="Selected")
def chooseCompareFile():
global compare_file_path
global accuracy_holder
accuracy_holder = None
compare_file_path = filedialog.askopenfilenames(filetypes=[("Excel files", ".docx .doc")])
if compare_file_path != '':
compare_path_label.config(text="File Selected!" , fg="green")
choose_compare_file_btn.config(bg="blue", text="Selected")
def start_compare():
global accuracy
global accuracy_holder
if main_file_path == None or main_file_path == '' or compare_file_path == None or compare_file_path == '':
if main_file_path == None or main_file_path == '':
messagebox.showinfo("showinfo", "Please Choose Main File")
if compare_file_path == None or compare_file_path == '':
messagebox.showinfo("showinfo", "Please Choose Compare File")
return
elif len(main_file_path) != len(compare_file_path):
messagebox.showinfo("showinfo", "Please Choose Equal Files")
return
with concurrent.futures.ThreadPoolExecutor() as executor:
start_comparision_btn.config(text="Comparing...Please Wait" , bg="blue")
root.update()
future = executor.submit(compare)
return_value = future.result()
accuracy_holder = return_value
if return_value < 0:
accuracy_holder = 0
accuracy.config(text = "Accuracy = "+str(accuracy_holder)+"%")
start_comparision_btn.config(text="File Compared Successfully!" , bg="green")
generate_pdf_btn.pack(pady=10)
reset_btn.pack()
Label(text="Compare Docx" , font =("Arial",20,"bold")).pack(pady=15)
Label(text="Choose Main File" , font =("Arial",15,"italic")).pack(pady=10)
choose_main_file_btn = Button(text="Choose" , fg="white" , bg="black" , padx=20 ,borderwidth=0 ,command=chooseMainFile)
choose_main_file_btn.pack(pady=10)
main_path_label = Label(text="File Not Selected" , font =("Arial",10,"italic") , fg="red")
main_path_label.pack()
Label(text="Choose Compare File" , font =("Arial",15,"italic")).pack(pady=10)
choose_compare_file_btn = Button(text="Choose" , fg="white" , bg="black" , padx=20 ,borderwidth=0 ,command=chooseCompareFile)
choose_compare_file_btn.pack(pady=10)
compare_path_label = Label(text="File Not Selected" , font =("Arial",10,"italic") , fg="red")
compare_path_label.pack()
start_comparision_btn = Button(text="Start Comparison" , fg="white" , bg="black" , padx=20 ,borderwidth=0 ,command= lambda : start_compare())
start_comparision_btn.pack(pady=10)
accuracy = Label(text="" , font =("Arial",25,"italic"))
accuracy.pack(pady=10)
generate_pdf_btn = Button(text="Generate PDF" , fg="white" , bg="green" ,borderwidth =0 , padx=60 , pady=10, command=generate_pdf)
reset_btn = Button(text="RESET" , fg="white" , bg="red" ,borderwidth =0 , padx=60 , command=reset_all)
root.mainloop()