该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import os
from win32com import client
import multiprocessing
import pythoncom
import time
#excel文件遍历
def visitDir(path,filepaths):
for root,dirs,files in os.walk(path):
for filepath in files:
if os.path.splitext(filepath)[1]=='.xlsm':
filepaths.put(os.path.join(root,filepath))
#excel添加访问密码
def addPassword(filepaths):
print(".....")
lock=multiprocessing.Lock()
while not filepaths.empty():
try:
lock.acquire()
xl_name=filepaths.get()
lock.release()
xl_name=xl_name.replace('/','\\')
pythoncom.CoInitialize()
xlApp = client.DispatchEx("Excel.Application")
xlbook = xlApp.Workbooks.Open(xl_name)
xlbook.password='2208'
xlbook.Save()
xlbook.Close()
pythoncom.CoUninitialize()
print("文件加密成功,文件路径:",xl_name)
except:
print("文件加密失败,文件路径:",xl_name)
finally:
xlbook.Close()
if __name__=='__main__':
filepaths=multiprocessing.Queue()
root=r'C:\Users\Administrator\Desktop\TEST'
visitDir(root,filepaths)
start=time.time()
p1=multiprocessing.Pool(2)
for i in range(2):
p1.apply_async(addPassword,(filepaths,))
p1.close()
p1.join()
print(time.time()-start)
以上代码为什么多进程直接结束,不执行addPassword函数???