This article is an example of Python PDF file operation. Shared for your reference, as follows:
The title is as follows:
- use the ninth chapter of the () function to write a script, traversing the folder all the pdf, with the command line to provide the command to encrypt these PDF, with the original filename plus _encrypted.pdf suffix, to save each encrypted PDF. before deleting the original file, try to use the program to read and decrypt the file, to ensure that it is encrypted correctly
- Then write a program that finds all the encrypted PDF files in the folder and creates a decrypted copy of the pdf, using the passphrase provided. If the passphrase is not correct, the program should print a message.
And move on to the next pdf file
The thought process is as follows:
- Function 1 within the program needs to do the following:
Find all PDF files in a folder
Encrypt PDF files
Save encrypted PDF files
Verify correct encryption
Deleting source files
- In-program function 2 needs to do the following:
Iterate through the folder with the _encrypted suffix of all PDF files
Open with the provided passphrase
If it can be opened correctly, then copy the password and save it to a txt file.
Cannot open the output to the screen correctly
- The code needs to do the following:
import os,PyPDF2,sys,send2trash
Generate a new folder for saving encrypted PDFs and copying text.
- Write a cryptographic function
In-function call () traverses the folder and saves the filename to the list
The command line parameter () provides an encrypted passphrase
For loop for file encryption and save encrypted file operations
decrypt for decryption, ensure correct encryption, and provide feedback
Delete original file (send2trash) - Write a generate-decrypt copy function
() traversal, decrypt for decryption, feedback results, generate password copy txt
try-except for decrypt control, decrypt failed to print a message, continue to continue
The code is as follows:
Since my code prompts for a PyPDF2 module when running from the command line, the string used for the command line argument is put directly into the function instead.
#! python3 import os, sys, PyPDF2,send2trash (".\\NewPDF") print("Folder created successfully!") path1 = (".\\NewPDF") # File encryption functions def decryptFile(argv,p = (".\\New")): tagFloder = '.\\PDF' pdfList = [] # Create a new folder in the current directory #(".\\NewPDF") # Iterate through the target folder and add the .pdf file names to the list for foldername, subfolders, filenames in (tagFloder): for filename in filenames: if ('.pdf'): (filename) print('PDF file found: %s' %filename) else: continue #Encrypt pdf files) for i in pdfList: pdfFile = open((foldername,filename),'rb') pdfReader = (pdfFile) pdfWriter = () #Copy for pageNum in range(): ((pageNum)) newName = i[0:-4] #Split file names (argv) # Password encryption pdfResult = open("{0}\\{1}_encrypted.pdf".format(p,newName),"wb+") #Creating a new filename object (pdfResult) # Write to new file pdfReader2 = (pdfResult,'rb') #Cryptographic confirmation if (argv): print("Correct encryption! Delete the original file in...") try: send2trash.send2trash((foldername,i)) except: print("Deleting original file: %s Failed!"%i) () print("Done!") # password copy function def copyDcrypt(argv,p = (".\\New")): pdfList = [] for foldername, subfolders, filenames in ("."): print("Parent folder: %s"%foldername) for filename in filenames: if (".pdf"): pdfReader = (open((foldername,filename),'rb')) if : (filename) print('Encrypted PDF file found: %s' % filename) else: continue for i in pdfList: newName = i[0:-4] #Split file names try: if (argv) == 1: copyFile = open("{0}\\{1}_PASSWORD.txt" .format(p,newName), 'w') ("Password is : %s" % argv) () print("The password is correct! Copy generated successfully!") else: print("Wrong password!") except: continue # Calling functions decryptFile("ABCDEFG",path1) copyDcrypt("ABCDEFG",path1)
Running results:
-
The pycharm interface runs the results:
- Original folder:
- New folder:
- Copy the text file:
For more Python related content, readers can check out this site's topic:Summary of Python file and directory manipulation techniques》、《Summary of Python coding manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials》
I hope the description of this article will help you in Python programming.