• <small id='ppQ9i'></small><noframes id='ppQ9i'>

    <tfoot id='ppQ9i'></tfoot>
      <i id='ppQ9i'><tr id='ppQ9i'><dt id='ppQ9i'><q id='ppQ9i'><span id='ppQ9i'><b id='ppQ9i'><form id='ppQ9i'><ins id='ppQ9i'></ins><ul id='ppQ9i'></ul><sub id='ppQ9i'></sub></form><legend id='ppQ9i'></legend><bdo id='ppQ9i'><pre id='ppQ9i'><center id='ppQ9i'></center></pre></bdo></b><th id='ppQ9i'></th></span></q></dt></tr></i><div id='ppQ9i'><tfoot id='ppQ9i'></tfoot><dl id='ppQ9i'><fieldset id='ppQ9i'></fieldset></dl></div>
      <legend id='ppQ9i'><style id='ppQ9i'><dir id='ppQ9i'><q id='ppQ9i'></q></dir></style></legend>

        • <bdo id='ppQ9i'></bdo><ul id='ppQ9i'></ul>

        从 Python 获取活动 Excel 工作簿的名称

        时间:2023-09-11

        <legend id='18Zdr'><style id='18Zdr'><dir id='18Zdr'><q id='18Zdr'></q></dir></style></legend>
          <bdo id='18Zdr'></bdo><ul id='18Zdr'></ul>
          • <tfoot id='18Zdr'></tfoot>

                • <small id='18Zdr'></small><noframes id='18Zdr'>

                    <tbody id='18Zdr'></tbody>

                  <i id='18Zdr'><tr id='18Zdr'><dt id='18Zdr'><q id='18Zdr'><span id='18Zdr'><b id='18Zdr'><form id='18Zdr'><ins id='18Zdr'></ins><ul id='18Zdr'></ul><sub id='18Zdr'></sub></form><legend id='18Zdr'></legend><bdo id='18Zdr'><pre id='18Zdr'><center id='18Zdr'></center></pre></bdo></b><th id='18Zdr'></th></span></q></dt></tr></i><div id='18Zdr'><tfoot id='18Zdr'></tfoot><dl id='18Zdr'><fieldset id='18Zdr'></fieldset></dl></div>
                  本文介绍了从 Python 获取活动 Excel 工作簿的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试编写一个 Python 脚本,该脚本将使用 Excel COM 接口访问和修改活动的 Excel 工作簿.但是,当有多个 Excel 实例正在运行时,我很难让它工作.比如代码

                  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)
                  

                  仅从第一个运行的 Excel 实例中打印出活动工作簿的名称.我真正想要的是我上次单击的工作簿,无论它位于哪个 Excel 实例中.

                  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.

                  谢谢.

                  推荐答案

                  编辑评论

                  可能有更好的方法来做到这一点.

                  There might be a better way to do this.

                  安装优秀的psutil

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

                  现在枚举窗口,但获取窗口标题和 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)
                  

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

                  Now just find the first pid that is in our excelPids

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

                  结束编辑

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

                  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.

                  或者是否有单独的 EXCEL.EXE 实例正在运行?您可以通过获取每个实例:

                  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.

                  对于上面的@tgrays 评论,如果您的 excel 实例保证是前台窗口,那么:

                  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
                  

                  祝你好运!

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

                  上一篇:COM:excelApplication.Application.Quit() 保留进程 下一篇:Python:在没有剪贴板的情况下从 Office/Excel 文档访

                  相关文章

                  最新文章

                • <legend id='v9iSl'><style id='v9iSl'><dir id='v9iSl'><q id='v9iSl'></q></dir></style></legend>
                  • <bdo id='v9iSl'></bdo><ul id='v9iSl'></ul>

                  <small id='v9iSl'></small><noframes id='v9iSl'>

                  <i id='v9iSl'><tr id='v9iSl'><dt id='v9iSl'><q id='v9iSl'><span id='v9iSl'><b id='v9iSl'><form id='v9iSl'><ins id='v9iSl'></ins><ul id='v9iSl'></ul><sub id='v9iSl'></sub></form><legend id='v9iSl'></legend><bdo id='v9iSl'><pre id='v9iSl'><center id='v9iSl'></center></pre></bdo></b><th id='v9iSl'></th></span></q></dt></tr></i><div id='v9iSl'><tfoot id='v9iSl'></tfoot><dl id='v9iSl'><fieldset id='v9iSl'></fieldset></dl></div>

                  1. <tfoot id='v9iSl'></tfoot>