forked from chenguohui/AutomatePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatchEncryptPdf.py
More file actions
35 lines (29 loc) · 1.1 KB
/
batchEncryptPdf.py
File metadata and controls
35 lines (29 loc) · 1.1 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
import PyPDF2
import os
path = ''
password = ''
# get all pdf file in pointed directory
for dirpath, dirnames, filenames in os.walk(path):
# open each pdf file and get first page of it
for filename in filenames:
if not filename.endswith('.pdf'):
continue
filepath = os.path.join(dirpath, filename)
pdfFile = PyPDF2.open(filepath, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
# if catch Exception then decrypt the file with given pass
try:
pdfReader.getPage(0)
except err:
if pdfReader.decrypt(password):
pdfWriter = PyPDF2.PdfFileWriter()
for page in pdfReader.numPages:
pdfWriter.addPage(pdfReader.getPage(0))
decryptPdfFile = open(destDirectory+filename+'_encrypted.pdf', 'wb')
pdfWriter(decryptPdfFile)
decryptPdfFile.close()
else:
# if pass is error, the print message and continue
print(msg)
continue
pdfFile.close()