Pywinauto:如何在程序上设置焦点窗口-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从自定义软件复制数据后,如何设置Pywinauto将对Excel程序本身“设置焦点”?谢谢。

解决方法

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

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

使用win = app.window(title_re='.*Excel')type_keys,如Vasily在评论中所述。

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}")

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...