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
68
69
|
import win32com.client
import os
import win32api, win32con
def find_file(l):
path = os.getcwd()
#print(path)
Files = os.listdir(path)
for k in range(len(Files)):
if Files[k].endswith('xlsx') or Files[k].endswith('xls') or Files[k].endswith('xlsm'):
l.append(os.path.join(path, Files[k]))
else:
continue
return l
def delete_module(xlwb):
for i in xlwb.VBProject.VBComponents:
xlmodule = xlwb.VBProject.VBComponents(i.Name)
if xlmodule.Type in [1, 2, 3]:
if i.Name == 'Kangatang':
xlwb.VBProject.VBComponents.Remove(xlmodule)
n = n +1
#print("remove xlmodule")
if os.path.exists(exist_file):
os.remove(exist_file)
#print("remove excelfile")
exist_file = 'C:\\Users\\{0}\\AppData\\Roaming\\Microsoft\\Excel\\XLSTART\\mypersonnel.xls'.format(os.getlogin())
l = []
pathlis = find_file(l)
global n
n = 0
xlApp = win32com.client.Dispatch("Excel.Application")
xlwb = None
#创建路径下文件名
#遍历path列表,1空列表提示,2excel文件加到列表
#print(path)
# ITERATE THROUGH EACH VB COMPONENT (CLASS MODULE, STANDARD MODULE, USER FORMS)
try:
print(pathlis)
if pathlis == []:
win32api.MessageBox(0 ,"请将文件放到有excel的目录里","消息框",win32con.MB_OK)
# OPEN EXCEL APP AND WORKBOOK
#win32api.MessageBox(0 ,"当前路径为"+path,"消息框",win32con.MB_OK)
else:
for path in range(len(pathlis)):
print(path)
xlwb = xlApp.Workbooks.Open(pathlis[path])
delete_module(xlwb)
xlwb.Close(True)
if n != 0:
win32api.MessageBox(0,"共删除模块{0}次".format(n),"消息框", win32con.MB_OK)
else:
win32api.MessageBox(0,"没有发现问题文件","消息框", win32con.MB_OK)
except Exception as e:
print(e)
#win32api.MessageBox(0, e ,"消息框", win32con.MB_OK)
finally:
# CLOSE AND SAVE AND UNINITIALIZE APP
xlApp.Quit
xlApp = None
|