Python batch encrypts excel files

Sometimes we need to regularly send emails to outside the company, and the files need to be encrypted and transmitted when automatically sending emails. This article will explore with you how to use python to encrypt single files and batch files.
  
For automated email sending in Python, please refer to [Dry Information] Use Python to send monitoring emails regularly every day .

1. Install the pypiwin32 package

  
To call win32com.client, you need to install the pypiwin32 package first. Open cmd and the installation statement is as follows:

pip install pypiwin32

If the installation is successful, the results will be displayed as follows:
  
Insert image description here

  
  

2. Define functions for excel encryption

  
We first define a function to encrypt a single excel. The specific code is as follows:

import win32com.client

#excel加密函数
def pwd_xlsx(old_filename, new_filename, pwd_str, pw_str=''):
    '''
    old_filename:旧文件名 
    new_filename:新文件名
    pwd_str:保存的新文件密码
    pw_str:打开文件密码,若无访问密码,则设为''
    '''    
    xcl = win32com.client.Dispatch("Excel.Application")
    wb = xcl.Workbooks.Open(old_filename, False, False, None, pw_str)
    xcl.DisplayAlerts = False
    #关闭显示告警对话框
    wb.SaveAs(new_filename, None, pwd_str, '')
    #保存时可设置访问密码
    xcl.Quit()

The main function is to input old files, generate new files, and encrypt new files.
  
  

3. Encrypt a single excel

  
Before batch encryption, call the function in the previous section to encrypt a single file. The specific code is as follows:

filename = "F:\\公众号\\71.excel自动加密\\train_u6lujuX_CVtuZ9i.csv"
pwd_xlsx(filename,filename, '1111')
# 三个参数:待加密文件,加密后名称,密码

Result obtained:
  
Insert image description here
  
From the above pwd_xlsx function call statement, it can be seen that the file to be encrypted and the encrypted file are the same, and the set file password is 1111 (the password can be set as needed). That is, the original file is opened, the new file is saved and encrypted. Since the new file has the same name as the original file, the original file is actually overwritten with the encrypted new file.
  
Note 1: I tried to use the os.chdir function to define the folder where the original files are stored, and only put the file name in the pwd_xlsx function, but an error was reported.
  
Note 2: This function can encrypt excel files. If the input is a csv file, although no error will be reported, the generated file will not be encrypted.
  
If you want the encrypted file not to overwrite the original file, the specific code is as follows:

old_filename = "F:\\公众号\\71.excel自动加密\\date2.xlsx"
new_filename = "F:\\公众号\\71.excel自动加密\\date2_m.xlsx"
pwd_xlsx(old_filename, new_filename, '123')
# 三个参数:待加密文件,加密后名称,密码

got the answer:
  
Insert image description here
  
  

4. Encrypt excel in batches

  
First put the files that need to be encrypted into a folder, and use the following program to get the names of all the files in the folder.

import os

folder_path = "F:\\公众号\\71.excel自动加密\\批量加密"
#文件夹路径
file_list = os.listdir(folder_path)
#获取文件夹中所有文件的文件名
for file_name in file_list:
    print(file_name)
    #打印文件夹中的文件名

got the answer:

pl_date1.xlsx
pl_date1_m.xlsx
pl_date2.xlsx
pl_date3.xlsx
pl_date4.xlsx
pl_date5.xlsx

The original folder contents are as follows:
  
Insert image description here
  
It can be found that the two are consistent. Then adjust the above code to generate the original file path and name, as well as the new file path and name, and call the encryption function in batches to generate encrypted files. The specific code is as follows:
  

import os

folder_path = "F:\\公众号\\71.excel自动加密\\批量加密"
#文件夹路径
file_list = os.listdir(folder_path)
#获取文件夹中所有文件的文件名
for file_name in file_list:
    old_filename = folder_path + '\\' + file_name
    #原始文件
    new_filename = old_filename.replace('.xlsx', '_m.xlsx')
    #新加密文件
    print(old_filename)
    print(new_filename)
    pwd_xlsx(old_filename, new_filename, '123')
    #调用加密函数对原始文件进行加密并生成新文件,文件密码123

got the answer:
  
Insert image description here

At this point, the explanation of batch encryption of excel files in Python has been completed. Interested friends can try the pictures themselves by following the code.
  
[Free group membership for a limited time] Discuss learning Python, playing with Python, risk control modeling, artificial intelligence learning, data analysis, etc. in the group for free, and you can also exchange related problems encountered at work. Friends who need it can add WeChat ID 19967879837, and add a note about the group you want to join, such as risk control modeling.
  
You may be interested in:
Using Python to draw Pikachu
, Python to draw word clouds,
Python to draw 520 Eternal Heartbeat,
Python face recognition - I only have you in my eyes,
Python to draw a beautiful starry sky chart (beautiful background)
[Python] Valentine's Day Confession Fireworks ( With sound and text)
Use the py2neo library in Python to operate neo4j to build a correlation graph
Python romantic confession source code collection (love, roses, photo wall, confession under the starry sky)

Guess you like

Origin blog.csdn.net/qq_32532663/article/details/132262006