-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt_pdf.py
More file actions
executable file
·47 lines (36 loc) · 1.27 KB
/
decrypt_pdf.py
File metadata and controls
executable file
·47 lines (36 loc) · 1.27 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
#!/usr/bin/python3
from PyPDF2 import PdfFileWriter, PdfFileReader
import sys
import os
# Create a PdfFileWriter object
out = PdfFileWriter()
# Open encrypted PDF file with the PdfFileReader
file = PdfFileReader(sys.argv[1])
# Store correct password in a variable password.
password = sys.argv[2]
# Check if the opened file is actually Encrypted
if file.isEncrypted:
try:
# If encrypted, decrypt it with the password
file.decrypt(password)
# Now, the file has been unlocked.
# Iterate through every page of the file
# and add it to our new file.
for idx in range(file.numPages):
# Get the page at index idx
page = file.getPage(idx)
# Add it to the output file
out.addPage(page)
# Open a new file
with open("decrypted_"+sys.argv[0], "wb") as f:
# Write our decrypted PDF to this file
out.write(f)
# Print success message when Done
print("File decrypted Successfully.")
except NotImplementedError:
command=f"qpdf --password='{sys.argv[2]}' --decrypt {os.getcwd()+'/'+sys.argv[1]} {os.getcwd()+'/'+'decrypt_'+sys.argv[1]};"
os.system(command)
else:
# If file is not encrypted, print the
# message
print("File already decrypted.")