Pywinauto:如何在程序上设置焦点 window - Excel

Pywinauto: How to set focus window on a program - Excel

我是 Python 的新手,最近我们需要自动化仪表的测量读数。 因此,我们使用 Pywinauto 来自动化这些任务。

我们需要将测量值从仪表自定义软件复制并粘贴到 Excel 程序本身,我写了这样的东西。

from pywinauto import Application
from pywinauto import findwindows
from pywinauto.keyboard import send_keys


#Block of code that read data from the meter

#Starting the app
app = Application(backend="uia")
#app.start(r"C:/Program Files/Microsoft Office/root/Office16/EXCEL.exe")
app.connect(path=r"C:/Program Files/Microsoft Office/root/Office16/EXCEL.exe")


#Start - Block of Codes to focus on Excel programs
#???
#End - Block of Codes to focus on Excel programs

#Pseudocode of getting data from the meter
c = "9.8651"
send_keys(c) # to type PYWINAUTO
send_keys("{VK_RIGHT}") #Offset to right

如何设置 Pywinauto 在从自定义软件复制数据后将“设置焦点”在 Excel 程序本身?谢谢。

编辑:Vasily 提供了更优雅的解决方案。根据他的建议更改了答案。

以上解决方案我已经有了答案:

按照 Vasily 在评论中提到的那样使用 win = app.window(title_re='.*Excel')type_keys

from pywinauto import Application
from pywinauto import findwindows



#Block of code that read data from the meter

#Starting the app
app = Application(backend="uia")
#app.start(r"C:/Program Files/Microsoft Office/root/Office16/EXCEL.exe")
app.connect(path=r"C:/Program Files/Microsoft Office/root/Office16/EXCEL.exe")


#Start - Block of Codes to focus on Excel programs
win = app.window(title_re='.*Excel')
#End - Block of Codes to focus on Excel programs

#Pseudocode of getting data from the meter
c = "9.8651"
win.type_keys(c)
win.type_keys("{VK_RIGHT}")