• 欢迎光临~

(待完善)python3判断excel文件是否被打开,如果已经打开计算出来打开了几个,并且获取到打开excel的列表,并且获取到et.exe的进程pid

开发技术 开发技术 2022-10-08 次浏览

该部分代码还需要完善

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import psutil
 5 from win32com.client import Dispatch
 6 
 7 
 8 def fileisopen(filepath):
 9   xlslist = []
10   excelapp = Dispatch("Excel.Application")
11   if excelapp.workbooks.count == 0:
12     return False
13   else:
14     print('打开Excel工作表个数:{}'.format(excelapp.workbooks.count))
15     for i in range(1, excelapp.workbooks.count + 1):
16       realpth = excelapp.workbooks(i).Path + '\' + excelapp.workbooks(i).Name
17       if excelapp.workbooks.count > 1:
18         xlslist.append(realpth)
19       elif excelapp.workbooks.count == 1:
20         print('打开文件路径:{}'.format(realpth))
21         if realpth.lower() == filepath.lower():
22           return True, realpth
23     return False, realpth, xlslist
24 
25 
26 def get_pid(pname):
27   for proc in psutil.process_iter():
28     # print('pid-%d,name:%s' % (proc.pid, proc.name()))
29     if pname in proc.name():
30       return proc.pid
31 
32 
33 file_path = r'D:Python_Sctiptsxlwings_scrtipstest1.xls'
34 print('打开文件:{}'.format(fileisopen(file_path)))
35 
36 print(get_pid('et.exe'))

 

喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com