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