python自动移除excel文件密码(升级v2版本)-二具体代码实现

import msoffcrypto
import os
# 已加密Excel文件路径
encrypted_file = r"已加密Excel文件路径"
# 未加密文件将保存的路径
unencrypted_file = r"未加密文件将保存的路径"
# Excel文件可能的密码列表
excel_passwords = ["密码1", "密码2"]
def decrypt_excel(encrypted_file, unencrypted_file, excel_passwords):
    for password in excel_passwords:
        try:
            # 打开加密文件
            with open(encrypted_file, "rb") as encrypted_file_fd:
                office_file = msoffcrypto.OfficeFile(encrypted_file_fd)

                # 尝试使用当前密码解密
                office_file.load_key(password=password)

                # 如果没有抛出异常,说明密码正确,进行解密并跳出循环
                with open(unencrypted_file, "wb") as unencrypted_file_fd:
                    office_file.decrypt(unencrypted_file_fd)

                print(f"文件已使用密码'{password}'解密并保存至:{unencrypted_file}")
                break  # 密码正确,解密完成,结束循环

        except msoffcrypto.exceptions.InvalidKeyError:
            continue  # 当前密码错误,尝试下一个密码
        # 如果文件[Unencrypted document]没有加密直接就退出循环
        except msoffcrypto.exceptions.FileFormatError:
            # 没有加密的文件,直接复制操作
            with open(unencrypted_file, "wb") as unencrypted_file_fd:
                unencrypted_file_fd.write(open(encrypted_file, "rb").read())
            break
        except Exception as e:
            print(f"解密过程中发生错误:{e}")
            break  # 发生其他错误,结束循环
directory_path = r"G:\路径加密"
for root, dirs, files in os.walk(directory_path):
    for file in files:
        print(os.path.join(root, file))
        m=r'{}'.format(os.path.join(root, file))
        outm=r'{}'.format(os.path.join(root.replace('加密',''),file))
        decrypt_excel(m,outm,excel_passwords)
上一篇:同三维T80006系列编码器视频使用操作说明书:高清HDMI编码器,高清SDI编码器,4K超清HDMI编码器,双路4K超高清编码器


下一篇:Android下跨进程通信,网络通信机制,TCP/HTTP/SOCKET等协议简述