-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypter.py
82 lines (63 loc) · 2.79 KB
/
decrypter.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
import os
import PyPDF2
from tkinter import *
from tkinter import filedialog
# Create a GUI window using tkinter
root = Tk()
root.configure(background='grey')
root.title('PDF Password Remover')
root.geometry('300x350')
# this is the method select_file which when invoked uses filedialog to open a file and store the path of the file in the variable file_path
def select_file():
global file_path
file_path = filedialog.askopenfilename()
# Creating a label to show the file path
Label(root, text=file_path, bg='grey', fg='white').pack(pady=10)
# this is the method decrypt which when invoked decrypts the file using the password provided by the user
def decrypt():
passwd = password.get() # getting the password from the entry widget
# Opening the PDF file with a password
pdf_file = open(file_path, 'rb')
pdf_reader = PyPDF2.PdfReader(pdf_file)
if pdf_reader.is_encrypted:
pdf_reader.decrypt(passwd)
# Create a new PDF file without password protection
pdf_writer = PyPDF2.PdfWriter()
length = len(pdf_reader.pages)
for page_num in range(length):
pdf_writer.add_page(pdf_reader.pages[length - page_num - 1])
# Creating a new file with the name provided by the user
new_file_name = new_file.get()+".pdf"
new_pdf_file = open(new_file_name, 'wb')
pdf_writer.write(new_pdf_file)
# Closing the files
pdf_file.close()
new_pdf_file.close()
# Showing the file in a tkinter window
show_pdf()
# this is a method that has a canvas of 500x700 resizeable to show the pdf file
# def show_pdf():
# pdf_file = open(new_file.get()+".pdf", 'rb')
# pdf_reader = PyPDF2.PdfReader(pdf_file)
# page = pdf_reader.pages[0]
# page_content = page.extract_text()
# print(page_content)
# pdf_file.close()
def show_pdf():
pdf_file = new_file.get()+".pdf"
os.system(f"evince {pdf_file}")
# Button to choose the file to be decrypted
Button(root, text='Select File', bg='white', font=('arial', 12, 'normal'), command=select_file).pack(pady=20)
# Label widget to show the password
Label(root, text='Password: ', bg='grey', fg = 'white', font=('arial', 12, 'normal')).pack()
# Entry widget to accept the password
password = Entry(root,text='Password', width=20, font=('arial', 12, 'normal'), show='*', border=2)
password.pack(pady=20)
# Label widget to show the name with which the new file would be stored
Label(root, text='New File Name:', bg='grey', fg = 'white', font=('arial', 12, 'normal')).pack()
# Entry widget to accept the name with which the new file would be stored
new_file = Entry(root,text='New File Name', width=20, font=('arial', 12, 'normal'), border=2)
new_file.pack(pady=20)
# Button to decrypt the file
Button(root, text='Decrypt', bg='white', font=('arial', 12, 'normal'), command=decrypt).pack(pady=20)
root.mainloop()