Python进行Excel自动化操作中怎么关闭弹窗
Admin 2022-07-08 群英技术资讯 1124 次浏览
在实际应用中,我们有时候会遇到“Python进行Excel自动化操作中怎么关闭弹窗”这样的问题,我们该怎样来处理呢?下文给大家介绍了解决方法,希望这篇“Python进行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代码相应卡死在第一行。

主程序中不可能直接处理或者绕过此类问题,也不能奢望有人随时蹲守点击下一步――那就开启一个子线程来护航吧。因此,解决方案是利用子线程监听并随时关闭弹窗,直到主程序圆满结束。
解决这个问题,需要以下两个知识点(基础知识请课外学习):
pywinauto顾名思义是Windows界面自动化库,模拟鼠标和键盘操作窗体和控件 [^3]。不同于先获取句柄再获取属性的传统方式,pywinauto的API更加友好和pythonic。例如,两行代码搞定窗口捕捉和点击:
from pywinauto.application import Application win = Application(backend="win32").connect(title='Microsoft Excel') win.Dialog.Button.click()
本文采用自定义线程类的方式,启动线程后自动执行run()函数来完成上述操作。具体代码如下,注意构造函数中的两个参数:
# listener.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', 3)
t.start()
time.sleep(10)
t.stop()
于是,整个过程分为三步:
import xlwings as xw
from listener 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
整个方案和前文描述完全一致,只是替换MsgBoxListener类中关闭弹窗的方法:
import win32gui, win32con 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)来关闭弹窗。同理替换MsgBoxListener类的_close_msgbox()方法(同时构造函数中不再需要title参数):
def _close_msgbox(self):
'''Click any button ("OK", "Yes" or "Confirm") to close message box.'''
# get handles of all top windows
h_windows = []
win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), h_windows)
# check each window
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)
最后,实例演示结束全文,以后再也不用担心意外弹窗了。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要是为大家归纳整理了13个工作中常用到的pandas使用技巧,方便更高效地实现数据分析,感兴趣的小伙伴可以跟随小编一起学习一下
%r用rper()方法处理对象%s用str()方法处理对象函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式(如果没有
读万卷书不如行万里路,只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用Python开发一套校园管理系统,包含各种人员,如教师、学生等。学校的系统通常还包括一些课程的信息,大家可以在过程中查缺补漏,提升水平
每天上班最痛苦的事情就是早起早起早起!这是大部分上班族的痛苦,但是不上班又是不可能的啦,因为都是为了搞钱。本文用Python制作了三款有趣的闹钟,感兴趣的可以学习一下
这篇文章主要为大家介绍了Pytorch图像处理注意力机制解析及代码详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008