import os
from PyPDF2 import PdfFileReader, PdfFileWriter
folder = "C:/Users/Folder" # Directory containing files
in_file_name = "input.pdf" # Name of PDF to encrypt
out_file_name = "output (ENCRYPTED).pdf" # Name of output PDF
pw = "password" # Password to use
os.chdir(folder)
infile = file(in_file_name,"rb") # Read in PDF file
reader = PdfFileReader(infile)
writer = PdfFileWriter()
writer.appendPagesFromReader(reader)
writer.encrypt(pw) # Put passsowrd here
output_pdf = file(out_file_name, "wb")
writer.write(output_pdf)
print("Write successful!")