Skip to content

Commit b731cce

Browse files
committed
优化部分代码
1 parent 0f18700 commit b731cce

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

1.excel_decrypt.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ def excel_decrypt(src_file: str, password: str, del_src: bool = False)->bool:
1818
for pwd in passwords:
1919
try:
2020
xlapp = Dispatch("Excel.Application")
21-
xlapp.Workbooks.Open(src_file, False, True, None, pwd)
21+
wb = xlapp.Workbooks.Open(src_file, False, True, None, pwd)
2222
file_name = src_file.split("\\")[-1]
2323
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)))
24+
save_path = os.path.join(file_location, ("(decrypted)" + file_name))
25+
xlapp.DisplayAlerts = False
26+
xlapp.ActiveWorkbook.SaveAs(save_path, None, "", "")
27+
wb.Close()
2728
xlapp.Quit()
2829
flag = True
2930
print("decrypt success![%s]" % pwd)
@@ -32,19 +33,20 @@ def excel_decrypt(src_file: str, password: str, del_src: bool = False)->bool:
3233
os.remove(src_file)
3334
print("origin file delete success![%s]" % src_file)
3435
except Exception as e:
35-
print("origin file delete failed![%s]" % src_file)
36+
print("origin file delete failed![%s]" % src_file, repr(e))
3637
break
3738
except Exception as e:
38-
print("wrong password![%s]" % pwd)
39+
print("wrong password![%s]" % pwd, repr(e))
3940
else:
4041
try:
4142
xlapp = Dispatch("Excel.Application")
42-
xlapp.Workbooks.Open(src_file, False, True, None, password)
43+
wb = xlapp.Workbooks.Open(src_file, False, True, None, password)
4344
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)))
45+
file_location = src_file[0:(len(src_file) - len(file_name))]
46+
save_path = os.path.join(file_location, ("(decrypted)" + file_name))
47+
xlapp.DisplayAlerts = False
48+
xlapp.ActiveWorkbook.SaveAs(save_path, None, "", "")
49+
wb.Close()
4850
xlapp.Quit()
4951
flag = True
5052
print("decrypt success![%s]" % password)
@@ -53,12 +55,12 @@ def excel_decrypt(src_file: str, password: str, del_src: bool = False)->bool:
5355
os.remove(src_file)
5456
print("origin file delete success![%s]" % src_file)
5557
except Exception as e:
56-
print("origin file delete failed![%s]" % src_file)
58+
print("origin file delete failed![%s]" % src_file, repr(e))
5759
except Exception as e:
58-
print("wrong password![%s]" % password)
60+
print("wrong password![%s]" % password, repr(e))
5961
return flag
6062

6163

6264
if __name__ == "__main__":
6365
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))
66+
print(excel_decrypt(r"C:\Users\eason\Desktop\test\decrypt\t2.xlsx", password="111111,123456,121212", del_src=True))

0 commit comments

Comments
 (0)