Bottom Page

Thread Rating:
  • 3 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyPDF2 encrypt
#1
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.
Quote
#2
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()
Truman likes this post
Quote
#3
Executing now
Quote:print(pdfReader.isEncrypted)
it gives
Output:
False
.
Any idea why? I expected True.
Quote
#4
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
Truman likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Encrypt email message and forward entlearn 1 81 Jan-18-2019, 04:39 AM
Last Post: entlearn
  PyPDF2 Hanging When Trying to Open Corrupted PDF bmccollum 6 230 Nov-09-2018, 10:40 AM
Last Post: Larz60+
  PyPDF2 or indentation issue siten0308 3 521 Apr-20-2018, 09:35 PM
Last Post: siten0308
  PyPDF2 script problem mepyyeti 7 681 Mar-13-2018, 11:52 AM
Last Post: buran
  PyPDF2.PDFFileReader standenman 9 1,514 Feb-02-2018, 05:37 AM
Last Post: Larz60+
  PyPDF2: Find a PDF bookmark with a keyword Aviator83 1 1,542 Aug-01-2017, 06:35 PM
Last Post: Aviator83
  PyPDF2, merge.append(...) problem peterkl 1 2,316 Oct-23-2016, 09:50 AM
Last Post: peterkl

Forum Jump:


Users browsing this thread: 1 Guest(s)