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
  Need help understanding a couple of functions (encrypt,decrypt, int_to_bytes) xoani 0 70 Jun-09-2019, 03:25 PM
Last Post: xoani
  pyPDF2 nautilus columns modification AJBek 1 267 Jun-07-2019, 04:17 PM
Last Post: micseydel
  Using Pypdf2 write a string to a pdf file Pedroski55 6 350 Apr-11-2019, 11:10 PM
Last Post: snippsat
  Merging pdfs with PyPDF2 Pedroski55 0 162 Mar-07-2019, 11:58 PM
Last Post: Pedroski55
  Encrypt email message and forward entlearn 1 260 Jan-18-2019, 04:39 AM
Last Post: entlearn
  PyPDF2 Hanging When Trying to Open Corrupted PDF bmccollum 6 641 Nov-09-2018, 10:40 AM
Last Post: Larz60+
  PyPDF2 or indentation issue siten0308 3 853 Apr-20-2018, 09:35 PM
Last Post: siten0308
  PyPDF2 script problem mepyyeti 7 1,045 Mar-13-2018, 11:52 AM
Last Post: buran
  PyPDF2.PDFFileReader standenman 9 2,076 Feb-02-2018, 05:37 AM
Last Post: Larz60+
  PyPDF2: Find a PDF bookmark with a keyword Aviator83 1 2,085 Aug-01-2017, 06:35 PM
Last Post: Aviator83

Forum Jump:


Users browsing this thread: 1 Guest(s)