- 1、 Source code
- 1. Result display
- 2. Source code
- 2、 Source code comments
- 1. Dependent packages used
- 2. Function function
- 3. Code expansion
- 3、 Operational environment
1、 Source code
1. Result display
Python automated office is not a strange word now. As people pay more and more attention to their privacy, there will be such a demand: each person has a PDF file. Because they have personal privacy information, they need to encrypt the file and the password is the last six digits of their ID card.
no more nonsense. In conclusion, the program operation results are as follows:
let’s find out. The original document is as follows:
obtain the password as follows:
after encrypting the file, the results are as follows:
let’s open a random one to verify:
Enter the last six digits of the ID card as the password to open it.
2. Source code
The runnable source code is as follows:
import os from PyPDF2 import PdfFileWriter, PdfFileReader import pandas as pd #Global working directory Os.chdir ("D: \ encryption") def main(): #1. Find files #Obtain the file name of the original document of internship certificate Rosterfile = os.listdir ('. \ original file \') #Get the name and password of the table in the information name,Password = getPassword() #Encrypt and save each file for prove in rosterfile: for Name in name: if Name in prove: i = name.index(Name) rosterLast = prove Rosternow = '. \ file encryption version \ \' + prove password = Password[i] #2. Encrypt the file and output it encryptionPDF(rosterLast,rosterNow,password) #Obtained the name information in the table, and extracted the last six digits of the ID card as the password def getPassword(): password =  name =  The Excel column is obtained by name, ID number and identity card number. Name = pd.read_ Excel ('. \ list. Xlsx', usecols = , names = none) # reads the item name column without column name Id = pd.read_ Excel ('. \ list. Xlsx', usecols = , names = none) # reads the item name column without column name Name1 = Name.values.tolist() id = Id.values.tolist() #Extract the last six digits of the ID card as the password for i in id: password.append(str(i)[12:18:1]) #Extract the name as a string, which was originally list for j in Name1: name.append(j) return name,password #Encrypt files and output #Variable description: roserlast - original PDF file path, rosernow - encrypted output path, password - encrypted password def encryptionPDF(rosterLast,rosterNow,password): pdf_ Reader = pdffilereader (R '. \ original file \ \% s'% (rosterlast)) # enter the location / name of the PDF document you want to operate pdf_writer = PdfFileWriter() for page in range(pdf_reader.getNumPages()): pdf_writer.addPage(pdf_reader.getPage(page)) pdf_ Writer. Encrypt (password) # writes the password and outputs it to the destination path with open(rosterNow, 'wb') as out: pdf_writer.write(out) Print ("file:% s --- encryption completed, password:% s"% (rosterlast, password)) #Function entry if __name__ == '__main__': Print ("------ start encryption -----") main() Print ("------ encryption complete -----")
2、 Source code comments
1. Dependent packages used
Three functions are used:
#File operation import os #Operation PDF file from PyPDF2 import PdfFileWriter, PdfFileReader #Operation data import pandas as pd
Here I just briefly explain what I have done with these third-party libraries. For specific learning, please refer to the official documents or rookie tutorials.
2. Function function
Three functions are also defined in the coding process, and their functions are as follows:
#Main function, from here we can see how I implement this requirement as a whole def main(): #The function function is used to obtain the password of the file, and then match the name with the file name to meet the demand that my file is the last six digits of my ID card. def getPassword(): #The function function, the core of this coding, can be used as the preparation stage, that is, to provide the read, output path and password for this function to encrypt files. def encryptionPDF(rosterLast,rosterNow,password):
3. Code expansion
this is just a very simple requirement. It is not universal. It may only run on my computer. When you copy the source code, it may not run and will report all kinds of errors.
As like as two peas, if you want to use it directly, you may have to prepare for it exactly as I prepared in the encoding process. So it can only be used for learning and communication, and it is unrealistic to use it directly.
of course, you can try to add functions or increase the availability and threshold of code on this basis. For example, get the path entered by the user, define the password template, etc., so that the code is universal and can be copied and run directly.
3、 Operational environment
what if you don’t want to change the source code and want to use it directly?
no problem! Then follow me, bit by bit.
first create a folder named:Encryption; be careful! You must ensure that it is in the root directory! Its path is D: \ encrypted. Then open the file and create it as follows:It must be as like as two peas.
almost. The original files are placed in the original folder, and the files after running are in the encrypted version folder.
the most important thing is to fill in the information in the list form, which is used to set the password. The form format is as follows:ID number format is numeric.
the table has only two columns! A name and ID number. The ID number is digital.
This is the end of this article about Python’s implementation of batch file encryption. For more information about Python batch file encryption, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!