python 终端 不弹窗 python关不掉弹窗代码

您所在的位置:网站首页 专业烘干机设备有哪些 python 终端 不弹窗 python关不掉弹窗代码

python 终端 不弹窗 python关不掉弹窗代码

2024-05-13 14:13| 来源: 网络整理| 查看: 265

利用Python进行Excel自动化操作的过程中,尤其是涉及VBA时,可能遇到消息框/弹窗(MsgBox)。此时需要人为响应,否则代码卡死直至超时 12。根本的解决方法是VBA代码中不要出现类似弹窗,但有时我们无权修改被操作的Excel文件,例如这是我们进行自动化测试的对象。所以本文记录从代码角度解决此类问题的方法。

假想场景

使用xlwings(或者其他自动化库)打开Excel文件test.xlsm,读取Sheet1!A1单元格内容。很简单的一个操作:

import xlwings as xw wb = xw.Book('test.xlsm') msg = wb.sheets('Sheet1').range('A1').value print(msg) wb.close()

然而不幸的是,这个文件在打开工作簿时进行了热情的欢迎仪式:

Private Sub Workbook_Open() MsgBox "Welcome" MsgBox "to open" MsgBox "this file." End Sub

第一个弹窗Welcome就卡住了Excel,Python代码相应卡死在第一行。

python 终端 不弹窗 python关不掉弹窗代码_App

基本思路

主程序中不可能直接处理或者绕过此类问题,也不能奢望有人随时蹲守解决此类问题——那就开启一个子线程来坚守吧。因此,解决方案是利用子线程监听并随时关闭弹窗,直到主程序圆满结束。

Excel中MsgBox弹窗的默认标题是Microsoft Excel,接下来以此为例捕捉窗口,并通过点击按钮关闭它。

pywinauto方案

pywinauto顾名思义是Windows界面自动化库,模拟鼠标和键盘操作窗体和控件 3。不同于先获取句柄再获取属性的传统方式,pywinauto的API更加友好和pythonic。例如,两行代码就可以实现窗口捕捉和点击:

win = Application(backend="win32").connect(title='Microsoft Excel') win.Dialog.Button.click()

有关Python多线程的知识不在此展开,本文采用自定义线程类的方式,启动线程后自动执行run()函数。具体代码如下,构造函数中的两个参数:

title 需要捕捉的弹窗的标题,例如Excel弹窗默认为Microsoft Excel interval 监听的频率,即每隔多少秒就检查一次# listener_pywinauto.py import time from threading import Thread, Event from pywinauto.application import Application class MsgBoxListener(Thread): def __init__(self, title:str, interval:int): Thread.__init__(self) self._title = title self._interval = interval self._stop_event = Event() def stop(self): self._stop_event.set() @property def is_running(self): return not self._stop_event.is_set() def run(self): while self.is_running: try: time.sleep(self._interval) self._close_msgbox() except Exception as e: print(e, flush=True) def _close_msgbox(self): '''Close the default Excel MsgBox with title "Microsoft Excel".''' win = Application(backend="win32").connect(title=self._title) win.Dialog.Button.click() if __name__=='__main__': t = MsgBoxListener('Microsoft Excel', 1) t.start() time.sleep(10) t.stop()

于是,整个过程分为三步:

启动子线程监听弹窗主线程中打开Excel开始自动化操作关闭子线程import xlwings as xw from listener_pywinauto import MsgBoxListener # start listen thread listener = MsgBoxListener('Microsoft Excel', 3) listener.start() # main process as before wb = xw.Book('test.xlsm') msg = wb.sheets('Sheet1').range('A1').value print(msg) wb.close() # stop listener thread listener.stop()

到此问题基本解决,本地运行效果完全达到预期。但我的真实需求是以系统服务方式在服务器上进行Excel文件自动化测试,后续发现,当以系统服务方式运行时,pywinauto竟然捕捉不到弹窗!这或许是pywinauto一个潜在的问题 4。

win32gui方案

那就只好转向相对底层的win32gui(pywin32库的一部分),所幸解决了上述问题。

# pip install pywin32 import win32gui import win32con

以下仅列出MsgBoxListener类中关闭弹窗的步骤,其余代码完全一致:

def _close_msgbox(self): # find the top window by title hwnd = win32gui.FindWindow(None, self._title) if not hwnd: return # find child button h_btn = win32gui.FindWindowEx(hwnd, None,'Button', None) if not h_btn: return # show text text = win32gui.GetWindowText(h_btn) print(text) # click button win32gui.PostMessage(h_btn, win32con.WM_LBUTTONDOWN, None, None) time.sleep(0.2) win32gui.PostMessage(h_btn, win32con.WM_LBUTTONUP, None, None) time.sleep(0.2)

更一般地,当同时存在默认标题和自定义标题的弹窗时,就不便于根据标题进行捕捉。例如

MsgBox "Message with default title.", vbInformation, MsgBox "Message with title My App 1", vbInformation, "My App 1" MsgBox "Message with title My App 2", vbInformation, "My App 2"

那就扩大范围,尝试点击任何包含确定性描述按钮(例如OK,Yes,Confirm)来关闭窗口。

def _close_msgbox(self): '''Click button to close message box if has text "OK", "Yes" or "Confirm".''' # Get handles of all top wondows h_windows = [] win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), h_windows) for h_window in h_windows: # Get child button with text OK, Yes or Confirm of given window. h_btn = win32gui.FindWindowEx(h_window, None,'Button', None) if not h_btn: continue # check button text text = win32gui.GetWindowText(h_btn) if not text.lower() in ('ok', 'yes', 'confirm'): continue # click button win32gui.PostMessage(h_btn, win32con.WM_LBUTTONDOWN, None, None) time.sleep(0.2) win32gui.PostMessage(h_btn, win32con.WM_LBUTTONUP, None, None) time.sleep(0.2)

全文结束,以后再也不怕意外弹窗了。

Handling VBA popup message boxes in Microsoft Excel ↩︎Trying to catch MsgBox text and press button in xlwings ↩︎What is pywinauto ↩︎Remote Execution Guide ↩︎



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3