-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt-pdf.py
More file actions
25 lines (24 loc) · 1.28 KB
/
encrypt-pdf.py
File metadata and controls
25 lines (24 loc) · 1.28 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
import PyPDF2
pdfFile = open('meetingminutes.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
pdfWriter = PyPDF2.PdfFileWriter()
for pageNum in range(pdfReader.numPages):
pdfWriter.addPage(pdfReader.getPage(pageNum))
pdfWriter.encrypt('swordfish')
resultPdf = open('encryptedminutes.pdf', 'wb')
pdfWriter.write(resultPdf)
resultPdf.close()
# Before calling the write() method to save to a file, call the encrypt()
# method and pass it a password string. PDFs can have a user password
# (allowing you to view the PDF) and an owner password (allowing you to set
# permissions for printing, commenting, extracting text, and other features).
# The user password and owner password are the first and second arguments
# to encrypt(), respectively. If only one string argument is passed to encrypt(),
# it will be used for both passwords.
# In this example, we copied the pages of meetingminutes.pdf to a
# PdfFileWriter object. We encrypted the PdfFileWriter with the password
# swordfish, opened a new PDF called encryptedminutes.pdf, and wrote the
# contents of the PdfFileWriter to the new PDF. Before anyone can view
# encryptedminutes.pdf, they’ll have to enter this password. You may want to
# delete the original, unencrypted meetingminutes.pdf file after ensuring its
# copy was correctly encrypted.