简体   繁体   English

自动刷新 Excel 电子表格的 Python 脚本

[英]Python Script to Automate Refreshing an Excel Spreadsheet

I would like to automatically refresh my excel file.我想自动刷新我的 excel 文件。 Currently the code I have is not completely satisfactory because Excel asks me each time to click Cancel or OK.目前我的代码并不完全令人满意,因为 Excel 每次都要求我单击“取消”或“确定”。 I have two sheets in the Excel file.我的 Excel 文件中有两张工作表。

Here is the python code:这是python代码:

import win32com.client as win32
Xlsx = win32.DispatchEx('Excel.Application')
Xlsx.DisplayAlerts = True
Xlsx.Visible = True
book = Xlsx.Workbooks.Open('C:/Test_Excel/Essai_1.xlsx')
# Refresh my two sheets
book.RefreshAll()
book.Save()
book.Close()
Xlsx.Quit()
del book
del Xlsx

How to automatically refresh the Excel file without Excel asking me any questions.如何在 Excel 不问我任何问题的情况下自动刷新 Excel 文件。

Thank you谢谢

尝试将Xlsx.DisplayAlerts = True设置为Xlsx.DisplayAlerts = False ,这应该可以解决问题。

refreshall() is an async function so you need to wait for it to finish before you do anything else. refreshall() 是一个异步函数,因此您需要等待它完成才能执行其他任何操作。 you can use Xlsx.CalculateUntilAsyncQueriesDone() to accomplish this.您可以使用 Xlsx.CalculateUntilAsyncQueriesDone() 来完成此操作。

import win32com.client as win32
Xlsx = win32.DispatchEx('Excel.Application')
Xlsx.DisplayAlerts = True
Xlsx.Visible = True
book = Xlsx.Workbooks.Open('C:/Test_Excel/Essai_1.xlsx')
# Refresh my two sheets
book.RefreshAll()
Xlsx.CalculateUntilAsyncQueriesDone()# this will actually wait for the excel workbook to finish updating
book.Save()
book.Close()
Xlsx.Quit()
del book
del Xlsx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM