Python Pywinauto:如何在程序上设置焦点窗口-Excel
我是Python新手,最近我们需要自动化仪表的测量读数。 因此,我们使用Pywinauto自动化任务 我们需要复制和粘贴测量值从仪表自定义软件到Excel程序本身,我写了这样的东西Python Pywinauto:如何在程序上设置焦点窗口-Excel,python,pywinauto,Python,Pywinauto,我是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 #S
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程序本身上?谢谢。编辑:瓦西里提供了更优雅的解决方案。根据他的建议修改了答案 对于上述解决方案,我已经有了答案: 使用
win=app.window(title_re='.*Excel')
并按照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}")
win.type_keys(c)
的操作与win.set_focus()
和send_keys(c)
相同。我推荐win=app.window(title\u re='*Excel')
,因为top\u window()
是调用该方法时的顶级窗口。如果它被更改,您必须更新win
规范。嗨,瓦西里,谢谢您的评论,但我可能会在这里偶然发现错误,当我使用您的推荐代码时,我得到了以下错误:imgur.com/a/xinZNa6。你可以拿我的模块代码样本,自己在这里运行:愚蠢的老我。需要在*excel前面添加点哦,对不起,我也漏掉了点。:)