Skip to content

Commit c2ecf73

Browse files
committed
在知道excel文件可能密码的情况下进行自动化解密
1 parent 3b555da commit c2ecf73

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

1.excel_decrypt.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- coding: utf-8 -*-
2+
__author__ = "eason"
3+
from win32com.client import Dispatch
4+
import os
5+
6+
7+
def excel_decrypt(src_file: str, password: str, del_src: bool = False)->bool:
8+
"""
9+
Excel解密
10+
:param src_file:待解密Excel文件路径
11+
:param password:密码,多个密码用英文逗号隔开
12+
:param del_src:是否删除原始加密文件
13+
:return:
14+
"""
15+
flag = False
16+
if "," in password:
17+
passwords = password.split(",")
18+
for pwd in passwords:
19+
try:
20+
xlapp = Dispatch("Excel.Application")
21+
xlapp.Workbooks.Open(src_file, False, True, None, pwd)
22+
file_name = src_file.split("\\")[-1]
23+
file_location = src_file[0:(len(src_file) - len(file_name))]
24+
wb = xlapp.Workbooks[0]
25+
wb.Password = ""
26+
xlapp.ActiveWorkbook.SaveAs(os.path.join(file_location, ("(decrypted)" + file_name)))
27+
xlapp.Quit()
28+
flag = True
29+
print("decrypt success![%s]" % pwd)
30+
if del_src:
31+
try:
32+
os.remove(src_file)
33+
print("origin file delete success![%s]" % src_file)
34+
except Exception as e:
35+
print("origin file delete failed![%s]" % src_file)
36+
break
37+
except Exception as e:
38+
print("wrong password![%s]" % pwd)
39+
else:
40+
try:
41+
xlapp = Dispatch("Excel.Application")
42+
xlapp.Workbooks.Open(src_file, False, True, None, password)
43+
file_name = src_file.split("\\")[-1]
44+
file_location = src_file[0:(len(src_file)-len(file_name))]
45+
wb = xlapp.Workbooks[0]
46+
wb.Password = ""
47+
xlapp.ActiveWorkbook.SaveAs(os.path.join(file_location, ("(decrypted)"+file_name)))
48+
xlapp.Quit()
49+
flag = True
50+
print("decrypt success![%s]" % password)
51+
if del_src:
52+
try:
53+
os.remove(src_file)
54+
print("origin file delete success![%s]" % src_file)
55+
except Exception as e:
56+
print("origin file delete failed![%s]" % src_file)
57+
except Exception as e:
58+
print("wrong password![%s]" % password)
59+
return flag
60+
61+
62+
if __name__ == "__main__":
63+
print(excel_decrypt(r"C:\Users\eason\Desktop\test\decrypt\t1.xls", password="111111,123456,121212", del_src=True))
64+
print(excel_decrypt(r"C:\Users\eason\Desktop\test\decrypt\t2.xlsx", password="111111,123456,121212", del_src=False))

0 commit comments

Comments
 (0)