import win32com.client # 破解excel的模块
import win32ui # 打开文件夹对话框的引入模块
def get_sheetpw(xls, filename, password): ## 自定义函数
try: # 尝试
xlsheet = xls.Workbooks.Open(filename, False, True, None, Password=password)
print('破解成功!')
print("文档密码是:{}".format(password))
xlsheet.Close()
return True # 返回True
except: # 尝试不成功
return False # 返回False
if __name__ == '__main__':
xls = win32com.client.Dispatch("Excel.Application")
xls.DisplayAlerts = 0
p = 12300 ## 开始密码
print('破解中......')
##选择文件
dlg = win32ui.CreateFileDialog(1) # 参数 1 表示打开文件对话框
dlg.SetOFNInitialDir(r'C:\Users\Administrator\Desktop') # 设置打开文件对话框中的初始显示目录
dlg.DoModal()
filename = dlg.GetPathName() # 返回选择的文件路径和名称
while True:
# 选择文件,如不选择就指定文件
if filename:
## 因为p是数,所以只能打开数值的密码
isdone = get_sheetpw(xls, filename, p)
else:
## 因为p是数,所以只能打开数值的密码
isdone = get_sheetpw(xls, r'C:\Users\Administrator\Desktop\工作簿2.xlsx', p)
p = p + 1 ## 密码逐一加,知道长度的话可以指定数值开始
if isdone: ## 如果自定义函数能打开,则为true 结束while循环
break ## 退出while
- 1
- 2
前往页