如何让 Excel RefreshAll 等到完成才关闭?
How do I make an Excel RefreshAll wait to close until finished?
我正在尝试使用以下 Python 脚本刷新 Excel 文件:
xl = Dispatch('Excel.Application')
workbook = xl.Workbooks.open('\path\to\workbook.xlsx')
xl.Visible = True
workbook.RefreshAll()
xl.Quit()
但是,后台查询(连接到SQL数据库)需要一段时间才能刷新。
如何防止此电子表格在 RefreshAll 完成之前关闭?
对于 'query' & pivot tables,你可以这样做 你必须遍历每个 sheet & query/pivot** table 来设置背景刷新为 False,因为它是 table 本身的 属性(而不是在工作簿级别,比如说。)
**对于数据透视tables,Python模块中的数据透视表似乎不可迭代,所以我基本上设置了一个计数器(根据需要调整)来查看每个作品的最大(预期!)枢轴数 tablessheet(我将其设置为 5)。一旦找到从 1 到 5 的每个索引号没有枢轴 table,它就会停止。(这假设索引从 1 开始是连续的,并且你不能,比如说,没有数据透视表(1)但是有一个数据透视表(2)。如果那是错误的,请更正我的答案!
for sheet in workbook.Sheets:
print(sheet.name)
for table in sheet.QueryTables:
print("Found a query table on %s called %s" % (sheet.name, table.name))
table.BackgroundQuery = False # i.e.: disable background query, and therefore cause Excel to 'wait' until it's done refreshing
if table.Refresh() == True: # This both refreshes the table, AND if the Refresh() method returns True (i.e.: refreshes successfully), tells you so.
print("Query table %s refreshed!" % table.name)
else:
print("Query table %s FAILED to refresh." % table.name)
for i in range(1,5):
try:
print("Found pivot table #%s called %s" % (i,sheet.PivotTables(i).Name))
if sheet.PivotTables(i).RefreshTable()==True:
print("Refreshed pivot table %s" % sheet.PivotTables(i).Name)
else:
print("FAILED to refresh pivot table %s" % sheet.PivotTables(i).Name)
except:
print("No pivot table #%s found on sheet %s" % (i,sheet.Name))
break # Break out of the for loop, since there's no point in continuing the search for pivots, I believe (but could be wrong about that! Try creating two pivots, then deleting the first. Does the second become PivotTables(1), or stay as 2?)
CalculateUntilAsyncQueriesDone() 将暂停程序并等待刷新完成。
xl = Dispatch('Excel.Application')
workbook = xl.Workbooks.open('\path\to\workbook.xlsx')
xl.Visible = True
workbook.RefreshAll()
xl.CalculateUntilAsyncQueriesDone()
xl.Quit()
我正在尝试使用以下 Python 脚本刷新 Excel 文件:
xl = Dispatch('Excel.Application')
workbook = xl.Workbooks.open('\path\to\workbook.xlsx')
xl.Visible = True
workbook.RefreshAll()
xl.Quit()
但是,后台查询(连接到SQL数据库)需要一段时间才能刷新。
如何防止此电子表格在 RefreshAll 完成之前关闭?
对于 'query' & pivot tables,你可以这样做 你必须遍历每个 sheet & query/pivot** table 来设置背景刷新为 False,因为它是 table 本身的 属性(而不是在工作簿级别,比如说。)
**对于数据透视tables,Python模块中的数据透视表似乎不可迭代,所以我基本上设置了一个计数器(根据需要调整)来查看每个作品的最大(预期!)枢轴数 tablessheet(我将其设置为 5)。一旦找到从 1 到 5 的每个索引号没有枢轴 table,它就会停止。(这假设索引从 1 开始是连续的,并且你不能,比如说,没有数据透视表(1)但是有一个数据透视表(2)。如果那是错误的,请更正我的答案!
for sheet in workbook.Sheets:
print(sheet.name)
for table in sheet.QueryTables:
print("Found a query table on %s called %s" % (sheet.name, table.name))
table.BackgroundQuery = False # i.e.: disable background query, and therefore cause Excel to 'wait' until it's done refreshing
if table.Refresh() == True: # This both refreshes the table, AND if the Refresh() method returns True (i.e.: refreshes successfully), tells you so.
print("Query table %s refreshed!" % table.name)
else:
print("Query table %s FAILED to refresh." % table.name)
for i in range(1,5):
try:
print("Found pivot table #%s called %s" % (i,sheet.PivotTables(i).Name))
if sheet.PivotTables(i).RefreshTable()==True:
print("Refreshed pivot table %s" % sheet.PivotTables(i).Name)
else:
print("FAILED to refresh pivot table %s" % sheet.PivotTables(i).Name)
except:
print("No pivot table #%s found on sheet %s" % (i,sheet.Name))
break # Break out of the for loop, since there's no point in continuing the search for pivots, I believe (but could be wrong about that! Try creating two pivots, then deleting the first. Does the second become PivotTables(1), or stay as 2?)
CalculateUntilAsyncQueriesDone() 将暂停程序并等待刷新完成。
xl = Dispatch('Excel.Application')
workbook = xl.Workbooks.open('\path\to\workbook.xlsx')
xl.Visible = True
workbook.RefreshAll()
xl.CalculateUntilAsyncQueriesDone()
xl.Quit()