import io from msoffcrypto import OfficeFile # 解密工作簿 import tempfile # 临时文件 def decrypt_excel(file_path, pwd): '''# 解密Excel文件''' with open(file_path, "rb") as encrypted_file: office_file = OfficeFile(encrypted_file) if office_file.is_encrypted(): # Excel文件密码 # pwd = input("请输入密码:") try: office_file.load_key(password=pwd) decrypted_file = io.BytesIO() office_file.decrypt(decrypted_file) decrypted_file.seek(0) print('密码正确') return decrypted_file # 文件内容,内存地址 except Exception as e: print(f"Error: {e}") else: print("工作簿未加密") def decrypt_excel_to_tempfile(file_path, pwd): # 将解密的文件内容写入一个临时文件 try: decrypted_file = decrypt_excel(file_path, pwd) if decrypted_file: # 将解密的文件内容写入一个临时文件 with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp: tmp.write(decrypted_file.read()) return tmp.name # 临时文件路径 except: print("Error: 密码错误或文件已损坏")