从Python获取活动Excel工作簿的名称 [英] Get name of active Excel workbook from Python

查看:808
本文介绍了从Python获取活动Excel工作簿的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个Python脚本,将使用Excel COM接口访问和修改活动的Excel工作簿。但是,当我运行多个Excel实例时,我无法正常工作。例如,代码

  import win32com.client 

xl = win32com.client.Dispatch Excel.Application)
print(xl.ActiveWorkbook.FullName)

来自第一个运行的Excel实例的活动工作簿。我真正想要的是我最后点击的工作簿,不管它是什么Excel实例。



谢谢。

解决方案

编辑评论



可能有更好的方法。 / p>

安装优秀的 psutil



  import psutil 
excelPids = []
for psutil.process_iter():
if proc.name ==EXCEL.EXE:excelPids.append(proc.pid)

现在枚举窗口,但获取窗口标题和pid。

  windowPidsAndTitle = [] 
win32gui.EnumWindows(lambda hwnd, resultList:resultList.append((win32gui.GetWindowThreadProcessId(hwnd),win32gui.GetWindowText(hwnd))),windowPidsAndTitle)


$ b b

现在只需在我们的excelPids中找到第一个pid

  for pid,windowPidsAndTitle中的title:
if pid in excelPids:
return title

END EDITS / p>

这里有很多事情需要考虑:



一个实例有多个工作簿打开吗?在这种情况下

  xl = win32com.client.Dispatch(Excel.Application)
xl.ActiveWorkbook.FullName

确实会给你最后一个活动工作簿。



还是有单独的EXCEL.EXE实例运行? 您可以使用获取每个实例:

  xl = win32com.client.GetObjec(None,Excel.Application)#instance one 
xl = win32com.client.GetObject(Name_Of_Workbook)#instance two

但是这违反了目的,因为你需要知道名字,这不会告诉你最后一个



对于@tgrays注释,如果你的excel实例被保证为前台窗口,则:

  import win32gui 
win32gui.GetWindowText(win32gui.GetForegroundWindow())
#parse this并使用GetObject获取你的excel实例

但最糟糕的情况是scenerio,多个实例,你必须找到哪个焦点最后,你必须枚举所有的窗口, you care about:

  windows = [] 
win32gui.EnumWindows(lambda hwnd,resultList:resultList.append(win32gui .bat(hwnd)),windows)
#enumerates所有窗口从上到下打开
[i for windows在windows中如果Microsoft Excel在i] .pop(0)
#这一个最靠近顶部

祝你好运!


I am trying to write a Python script that will access and modify the active Excel workbook using the Excel COM interface. However, I am having difficulty getting this to work when there are multiple Excel instances running. For example, the code

import win32com.client

xl = win32com.client.Dispatch("Excel.Application")
print(xl.ActiveWorkbook.FullName)

prints out the name of the active workbook from the first running instance of Excel only. What I really want is the workbook that I last clicked on, regardless of what Excel instance it was in.

Thanks.

解决方案

EDIT FOR COMMENTS

There might be a better way to do this.

Install the excellent psutil

import psutil
excelPids = []
for proc in psutil.process_iter():
  if proc.name == "EXCEL.EXE": excelPids.append(proc.pid)

Now enumerate the windows, but get the window title and pid.

windowPidsAndTitle = []
win32gui.EnumWindows(lambda hwnd, resultList: resultList.append((win32gui.GetWindowThreadProcessId(hwnd),win32gui.GetWindowText(hwnd))), windowPidsAndTitle)

Now just find the first pid that is in our excelPids

  for pid,title in windowPidsAndTitle:
    if pid in excelPids:
      return title 

END EDITS

There is a number of things to take into consideration here:

Does one instance have multiple workbooks open? In this case

xl = win32com.client.Dispatch("Excel.Application")
xl.ActiveWorkbook.FullName

Will indeed give you the last active workbook.

Or are there separate instances of EXCEL.EXE running? You can get each instance with:

xl = win32com.client.GetObjec(None, "Excel.Application") #instance one
xl = win32com.client.GetObject("Name_Of_Workbook") #instance two

But this defeats the purpose because you need to know the name AND this will not tell you which one last had focus.

To @tgrays comment above, if your excel instance is guaranteed to be the foreground window then:

import win32gui
win32gui.GetWindowText(win32gui.GetForegroundWindow()) 
#parse this and use GetObject to get your excel instance

But worst case scenerio, multiple instances and you have to find which had focus last, you'll have to enumerate all the windows and find the one you care about:

windows = []
win32gui.EnumWindows(lambda hwnd, resultList: resultList.append(win32gui.GetWindowText(hwnd)),windows)
#enumerates all the windows open from the top down
[i for i in windows if "Microsoft Excel" in i].pop(0)
#this one is closest to the top

Good luck with this one!

这篇关于从Python获取活动Excel工作簿的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆