How To Encrypt Pdf Using Python?

Thumbnail

PDF ENCRYPT USING PYTHON


Want To Encrypt Your PDF With Password Using Python?

Here I am going to show you a example that how you can encrypt your pdf using PYPDF2 Module In Python

About PYPDF2

It is a Python Library that is used to work with PDFs

Like Encrypting and Decrypting PDFs, Splitting Pdfs, cropping Images and Many more.

You can Read More at : PYPDF2 Documentation

Installation

pip install PYPDF2

Code For Encrypting Pdf

from PyPDF2 import PdfFileWriter,PdfFileReader

class Encrypt:
    def __init__(self,filename,output_name,password):
        self.filename=filename
        self.output=output_name
        self.password=password

    def process(self):
        obj=PdfFileWriter()
        file=PdfFileReader(self.filename)
        if file.isEncrypted:
            print("PDF IS ALREADY ENCRYTED")
        else:
            for i in range(file.numPages):
                page=file.getPage(i)
                obj.addPage(page)
            obj.encrypt(self.password)
            with open(self.output,"wb") as f:
                obj.write(f)
            print("PDF ENCRYPTED SUCCESSFULLY...")

start=Encrypt("PDF NAME","NAME OF FINAL ENCRYPTED PDF","PASSWORD")      
start.process()  

0 Comments

Oldest