-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFparanoia.py
More file actions
67 lines (62 loc) · 2.85 KB
/
PDFparanoia.py
File metadata and controls
67 lines (62 loc) · 2.85 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
#! python3
# write a script that will go through every PDF in a folder (and its subfolders) and encrypt the PDFs using a
# password provided on the command line
import os, PyPDF2, send2trash
def encrypter(password, foldername):
print('Engaging the encrypter.')
for folder, subfolder, filename in os.walk(foldername):
print('searching for PDF file in %s' % folder)
for file in filename:
if file.endswith('.pdf'):
print('found %s.'% file)
pdfFile = open(os.path.join(foldername, file), 'rb')
pdfFileReader = PyPDF2.PdfFileReader(pdfFile)
if pdfFileReader.isEncrypted == True:
print('pdf file, %s is already encrypted, skipping...' % file)
continue
else:
newPdf = PyPDF2.PdfFileWriter()
for pageNum in range(pdfFileReader.numPages):
pageObj = pdfFileReader.getPage(pageNum)
newPdf.addPage(pageObj)
print('encrypting %s' % file)
newPdf.encrypt(password)
newPdfFile = open(os.path.join('encrypted PDFs', file+'_encrypted.pdf'), 'wb')
print('saving %s as %s_encrypted.pdf' %(file, file))
newPdf.write(newPdfFile)
pdfFile.close()
newPdfFile.close()
else:
continue
print('successfully encrypted all PDFs in %s' % foldername)
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)
pdfFile.close()
except:
print('Password incorrect for %s' % file)
else:
continue
else:
continue
def deleteFormerPdfs():
try:
decryptPdfs('encrypted PDFs', passwrd)
send2trash.send2trash('PDF folder')
except:
print('Something went wrong while trying to decrypt the files in encrypted PDFs folder.')
passwrd = input('Enter the password to be used for the files:\n')
encrypter(passwrd, 'PDF folder')
deleteFormerPdfs()