How do you protect a pdf in Python?

Viewed 24

I'm looking to password protect a PDF for editing, but without needing the password to view the file.

Is there a way to do this?

I looked at PyPDF2, but I could only find full encryption.

1 Answers

You can use the permisions flag. For example:

from PyPDF2 import PdfFileReader, PdfFileWriter

pdf_writer = PdfFileWriter()

# CREATE THE PDF HERE

# This is the key line, not the permission_flag parameter
pdf_writer.encrypt(user_pwd='', owner_pwd=PASSWORD, permissions_flag=0b0100)


with open('NewPDF.pdf', "wb") as out_file:
    pdf_writer.write(out_file)
Related