Popular python library PyPDF2 can be used to set password to PDF file. You can do many other things with this library like:

  • Cropping PDF pages
  • Parse PDF documents metadata (title, author, …)
  • Split pages
  • Merge pages
  • Multiple merge of pages
  • Encrypting and decrypting PDF files

Installation of library PyPDF2 is easy:

pip install pypdf2

The official page of the library is:
Home page for the PyPDF2 project

In this post we will use the PyPDF2 to protect a PDF file with a password.

This is the code example:

import PyPDF2 as p,os
output = p.PdfFileWriter()
input_stream = p.PdfFileReader(open("/home/user/Desktop/1.pdf", "rb"))

for i in range(0, input_stream.getNumPages()):
    output.addPage(input_stream.getPage(i))

outputstream = open("/home/user/Desktop/2.pdf", "wb")

output.encrypt("mypass", use_128bit=True)
output.write(outputstream)
outputstream.close()

The method which is used is encrypt. Below you can find it's header and the documentation:

    def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True):
        """
        Encrypt this PDF file with the PDF Standard encryption handler.

        :param str user_pwd: The "user password", which allows for opening
            and reading the PDF file with the restrictions provided.
        :param str owner_pwd: The "owner password", which allows for
            opening the PDF files without any restrictions.  By default,
            the owner password is the same as the user password.
        :param bool use_128bit: flag as to whether to use 128bit
            encryption.  When false, 40bit encryption will be used.  By default,
            this flag is on.
        """

user_pwd – The "user password", which allows for opening
and reading the PDF file with the restrictions provided.
owner_pwd – he "owner password", which allows for
opening the PDF files without any restrictions. By default,
the owner password is the same as the user password.
use_128bit – Decides which encryption to use128bit or 40bit.

You can control the pages in the output document like starting from page X:

for i in range(1, input_stream.getNumPages()):
    output.addPage(input_stream.getPage(i))

or removing some of the last pages by:

for i in range(0, input_stream.getNumPages() - 1):
    output.addPage(input_stream.getPage(i))