Installation package
pip isntall msoffcrypto-tool
A brief introduction to the msoffcrypto library
msoffcrypto provides the functionality to encrypt and decrypt Microsoft Office files. It supports encryption and decryption of Word, Excel and PowerPoint files.
The principle of msoffcrypto is to use the encryption algorithm of Microsoft Office files to encrypt and decrypt files. It can decrypt password-protected Office files and store the decrypted content in memory or other file streams.
Code
import os
import pandas as pd
import msoffcrypto
# pdRead encrypted files
def read_decrypt_file(file_path, password):
file_temp = io.BytesIO()
with open(file_path, "rb") as f:
file = msoffcrypto.OfficeFile(f)
# Determine whether there is a password
if file.is_encrypted():
file.load_key(password)
file.decrypt(file_temp)
else:
file_temp = file_path
# read file
# df = pd.ExcelFile(file_temp)
df = pd.read_excel(file_temp)
return df
if __name__ == '__main__':
file_path = "your own file path"
password = "123"
df = read_decrypt_file(file_path, password)
print(df)