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 me on LinkedIn, cirlce me on Google or follow on Twitter

  • manivarma indukuri

    Sir, I tried to give password protection to a PDF on your code but I encountered errors .
    Please help me Sir

  • hargurjeet

    Hi, I needed some assistance in passing the input file name and password. Can you illustrate an example for inputfile name…etc