在批量Excel文件中找出有密码保护的

import os
import win32com.client

xlApp = win32com.client.Dispatch("Excel.Application")
xlApp.Visible = False
xlApp.DisplayAlert = False
password = ''

folder_path = r"my_files"
normal_list = []
protected_list = []
except_list = []

for file_name in os.listdir(folder_path):
    file_path = os.path.join(folder_path, file_name)
    try:
        xlwb = xlApp.Workbooks.Open(file_path, UpdateLinks=False, ReadOnly=False, Format=None, Password=password, WriteResPassword=password)
        print(f"{file_name} is not protected")
        normal_list.append(file_name)
        xlwb.Close(True)
    except Exception as e:
        print(f"exception info: {e}")
        if "The password you supplied is not correct" in e.excepinfo[2]:
            print(f"{file_name} is protected")
            protected_list.append(file_name)
        else:
            print(f"{file_name} is neither normal nor protected only except")
            except_list.append(file_name)

print(f"\nthe protected files list is {protected_list}\nthe normal files list is {normal_list}\nthe files neither normal nor protected only except list is {except_list}")

脚本仅限于windows系统运行,可以筛选出加了密码保护的Excel和Word文件。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值