在单个excelsh中刷新连接的Python代码

2024-04-25 04:19:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的初学者。我在excel中编写了一些DBQ语句来获取 生成excel,每次打开excel时都应刷新该excel。在连接属性中给出了正确的设置。在

Below is my python code for refreshall:- 

import win32com.client  
import time
xl = win32com.client.DispatchEx("Excel.Application")
wb = xl.workbooks.open("D:\\Excel sheets\\Test_consolidation.xlsx")
xl.Visible = True
time.sleep(10)
wb.Refreshall()

我有3张表格在excel文件,它有3个不同的连接。我想一个接一个刷新。在

有人能帮我用python代码单独刷新连接吗?我将非常感谢你的帮助。在


Tags: importclient属性timeismy语句excel
1条回答
网友
1楼 · 发布于 2024-04-25 04:19:04

因此,如果您想一个接一个地刷新它们,而不是wb.Refreshall(),那么命令应该是:

for conn in wb.connections:
    conn.Refresh()

如果要将连接(例如在字典中)链接到工作表:

^{pr2}$

注意:如果一张纸有多个连接,这不是一个好方法。在

然后,如果只想更新特定工作表上的一个连接(在我的示例中称为Sheet1):

sheet_name = 'Sheet1'
# refresh the connection linked to the sheet_name 
# if existing in the dictionnary dict_conn_sheet
wb.connections(dict_conn_sheet[sheet_name]).Refresh() 

最后,如果您直接知道要更新的连接的名称(例如connection_Raj),只需输入:

name_conn = 'connection_Raj'
wb.connections(name_conn).Refresh()

我希望它是清楚的,即使它不能完全回答你的问题,因为我不知道我明白你想做什么。在

相关问题 更多 >