|
| 1 | +# Import the necessary libraries |
| 2 | +import PyPDF2, getpass # getpass is for getting password with some level of security |
| 3 | +from colorama import Fore, init |
| 4 | + |
| 5 | +# Initialize colorama for colored output |
| 6 | +init() |
| 7 | + |
| 8 | + |
| 9 | +# Function to lock pdf |
| 10 | +def lock_pdf(input_file, password): |
| 11 | + with open(input_file, 'rb') as file: |
| 12 | + # Create a PDF reader object |
| 13 | + pdf_reader = PyPDF2.PdfReader(file) |
| 14 | + |
| 15 | + # Create a PDF writer object |
| 16 | + pdf_writer = PyPDF2.PdfWriter() |
| 17 | + |
| 18 | + # Add all pages to the writer |
| 19 | + for page_num in range(len(pdf_reader.pages)): |
| 20 | + pdf_writer.add_page(pdf_reader.pages[page_num]) |
| 21 | + |
| 22 | + # Encrypt the PDF with the provided password |
| 23 | + pdf_writer.encrypt(password) |
| 24 | + |
| 25 | + # Write the encrypted content back to the original file |
| 26 | + with open(input_file, 'wb') as output_file: |
| 27 | + pdf_writer.write(output_file) |
| 28 | + |
| 29 | + |
| 30 | +# Get user input |
| 31 | +input_pdf = input("Enter the path to the PDF file: ") |
| 32 | +password = getpass.getpass("Enter the password to lock the PDF: ") |
| 33 | + |
| 34 | +# Lock the PDF using PyPDF2 |
| 35 | +print(f'{Fore.GREEN}[!] Please hold on for a few seconds..') |
| 36 | +lock_pdf(input_pdf, password) |
| 37 | + |
| 38 | +# Let the user know it's done |
| 39 | +print(f"{Fore.GREEN}[+] PDF locked successfully.") |
0 commit comments