-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfParanoiaEncrypt.py
More file actions
49 lines (30 loc) · 1.33 KB
/
pdfParanoiaEncrypt.py
File metadata and controls
49 lines (30 loc) · 1.33 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
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 24 14:39:38 2021
@author: martsc1
"""
# PDF paranoia encrypt
import os, PyPDF2
print('Enter folder tree directory in which you want to encrypt PDFs:')
folderTree = input()
print('Enter the password you want to encrypt PDFs with:')
password = input()
for folderName, subfolders, filenames in os.walk(folderTree):
for file in filenames:
if '.pdf' in file:
# create encrypted copy
pdfFile = open(folderName+'\\'+file, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
pdfWriter = PyPDF2.PdfFileWriter()
for pageNum in range(pdfReader.numPages):
pdfWriter.addPage(pdfReader.getPage(pageNum))
pdfWriter.encrypt(password)
encryptedPdf = open(folderName+'\\'+file[:-4]+'_encrypted.pdf', 'wb')
pdfWriter.write(encryptedPdf)
encryptedPdf.close()
pdfFile.close()
# check whether decryption
pdfCheck = PyPDF2.PdfFileReader(open(folderName+'\\'+file[:-4]+'_encrypted.pdf', 'rb'))
if pdfCheck.decrypt(password) == 1:
# delete original file
os.unlink(folderName+'\\'+file)