How to password protect PDF file using Python?

We will use python library PyPDF2 to set password to pdf file.

To install PyPDF2:

We are using encrypt function of PyPDF2.

encrypt(user_password, owner_password=None, use_128bit=True)

  • user_password  – The “user password” allows opening and reading the PDF file with the restrictions .
  • owner_password – The “owner password”  have no restrictions. By default, the owner password is the same as the user password.
  • use_128bit  – Decides which encryption to use128bit or 40bit.

If you have any queries, please comment!

About Gaurav Vichare

Hi , I am Gaurav : author of this blog , Software engineer and a Photoshop lover . You can connect with me on Facebook , cirlce me on Google or follow on Twitter