AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Python pdf reader11/9/2022 ![]() ![]() Else, we iterate over each page of the PDF file and add it to the pdf_writer object created. If it is already encrypted, we simply return a message from there. Inside that, we are first checking if the file is already encrypted or not. ![]() In the above code, we are creating a function encrypt_file. def encrypt_file(filename: str, password: str) -> str: pdf_writer = PdfFileWriter() pdf_reader = PdfFileReader(open(filename, 'rb'), strict=False) if is_encrypted(filename): return "PDF File is already encrypted." try: for page_number in range(pdf_reader.numPages): pdf_writer.addPage(pdf_reader.getPage(page_number)) except utils.PdfReadError: return "Error while reading PDF file" pdf_writer.encrypt(user_pwd=password, use_128bit=True) with open("encypted_demo.pdf", "wb") as f: pdf_writer.write(f) return "PDF file encrypted successfully" Now that we have a function ready to check whether the file is already encrypted or not, we can create a function that encrypts the file if it’s not. from PyPDF2 import PdfFileReader def is_encrypted(filename: str) -> bool: with open(filename, 'rb') as f: pdf_reader = PdfFileReader(f, strict=False) return pdf_reader.isEncrypted Encrypt PDF Fileįirst of all, let’s create a function that checks whether a file is already encrypted. For demo purposes, you can download this PDF file. Once installed, we are ready to work with it. #Python pdf reader installPyPDF2 is an external library and needs to be installed using the command: pip install PyPDF2 We’ll be using the PyPDF2 module to encrypt and decrypt our PDF files. In this blog, we’ll learn how can we set a password to protect a PDF file. Some examples of password-protected PDFs that we encounter in daily life are: ![]() Thus, it becomes necessary to encrypt its data or make it password-protected so that only genuine and authorized people can access it. But as we share it, there are high chances of its data being leaked or stolen. Various other important documents come in PDF format. If you are a student, you have scanned copies of assignments to submit, or your resume can be in PDF format. PDF documents can contain links and buttons, form fields, audio, video, and business logic. The PDF is now an open standard, maintained by the International Organisation for Standardisation (ISO). #Python pdf reader portableThat’s why we invented the Portable Document Format (better known by the abbreviation PDF), to present and exchange documents reliably - independent of software, hardware or operating system. You know you use PDFs to make your most important work happen. ![]()
0 Comments
Read More
Leave a Reply. |