如何在Python中使用win32com自动“确定”或绕过Excel中的弹出消息?

问题描述 投票:0回答:1

我正在尝试将一大组 .xls 文件的文件类型更改为 .xlsm,并不断收到弹出消息,在脚本继续之前我必须选择“确定”或“是”。

我已经尝试过time.sleep SendKeys方法,并且“ENTER”命令没有在Excel创建的窗口中注册。

我也尝试过 DisplayAlerts = False,但也没有成功。下面是我使用的代码,其中注释行是我尝试的:

os.chdir(r'C:\my\directory\location')
doc_path = r'C:\my\directory\location'

for root, dirs, files in os.walk(doc_path, topdown=False):
    for file in files:

        fname = file

        if fname.endswith('.xls'):
            excel = win32.gencache.EnsureDispatch('Excel.Application')
            #excel.DisplayAlerts = False
            wb = excel.Workbooks.Open(os.path.abspath(fname))
            #time.sleep(4)
            #excel.SendKeys("~",0)
            #excel.DisplayAlerts = False
            wb.SaveAs(fname+"m", FileFormat = 52) 
            #excel.DisplayAlerts = False
            wb.Close()                      
            #excel.DisplayAlerts = False
            excel.Application.Quit()        
            #excel.DisplayAlerts = False

        else:
            continue

关于如何解决这个问题有什么见解吗?

python excel python-3.x win32com
1个回答
0
投票

看起来您需要进入 Excel 并取消选中每个数据连接的“启用后台刷新”框。希望这有帮助

此操作将取消待处理的刷新数据命令

© www.soinside.com 2019 - 2024. All rights reserved.