Python Forum
PyPDF2 encrypt - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: PyPDF2 encrypt (/thread-15281.html)



PyPDF2 encrypt - Truman - Jan-11-2019

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.


RE: PyPDF2 encrypt - snippsat - Jan-11-2019

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()



RE: PyPDF2 encrypt - Truman - Jan-19-2019

Executing now
Quote:print(pdfReader.isEncrypted)
it gives
Output:
False
.
Any idea why? I expected True.


RE: PyPDF2 encrypt - snippsat - Jan-19-2019

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