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文件。