-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDF paranoia2.py
More file actions
36 lines (35 loc) · 1.71 KB
/
PDF paranoia2.py
File metadata and controls
36 lines (35 loc) · 1.71 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
#! python3
# a program that finds all encrypted PDFs in a folder (and its subfolders) and creates a decrypted copy of the PDF
# using a provided password.
import PyPDF2, os
def decryptPdfs(folder, password):
print('engaging the decrypter')
for foldername, subfolder, files in os.walk(folder):
print('searching for encrypted PDF files in %s' % folder)
for file in files:
if file.endswith('_encrypted.pdf'):
print('found %s' % file)
pdfFile = open(os.path.join(folder, file), 'rb')
pdfFileReader = PyPDF2.PdfFileReader(pdfFile)
if pdfFileReader.isEncrypted == True:
print('Decrypting %s...' % file)
pdfFileReader.decrypt(password)
try:
pdfFileReader.getPage(0)
print('successfully decrypted %s' % file)
newPdf = PyPDF2.PdfFileWriter()
for pageNum in range(pdfFileReader.numPages):
pageObj = pdfFileReader.getPage(pageNum)
newPdf.addPage(pageObj)
newPdfFile = open(os.path.join('unEncrypted_Pdfs', file.replace('_encrypted', '')), 'wb')
print('saving %s as %s' %(file, file.replace('_encrypted', '')))
newPdf.write(newPdfFile)
pdfFile.close()
newPdfFile.close()
except:
print('Password incorrect for %s' % file)
else:
continue
else:
continue
decryptPdfs('encrypted PDFs', 'BoluBabs')