Python Forum

Full Version: PyPDF2 encrypt
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to encrypt a pdf file but it gives syntax error
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('swordwish')
resultPdf = open('encryptedminutes.pdf', 'wb')
pdfWriter.write(resultPdf)
In line 7 I tried with alternative seen in docs PyPDF2.pdfWriter.encrypt('swordwish') but get the same Syntax error: invalid syntax.
Missing ) line 6.
Need to close file object for it to write.
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('swordwish')
resultPdf = open('encryptedminutes.pdf', 'wb')
pdfWriter.write(resultPdf)
resultPdf.close()
Executing now
Quote:print(pdfReader.isEncrypted)
it gives
Output:
False
.
Any idea why? I expected True.
It give False because now you check the file before is encrypted.
If open the encrypted encryptedminutes.pdf and test.
import PyPDF2

pdfFile = open('encryptedminutes.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
print(pdfReader.isEncrypted)
Output:
True