简体   繁体   English

用xlwings刷新Excel文件中的全部

[英]RefreshAll in excel file with xlwings

I wanted to RefreshAll database connections in a number of excel files but i didn't want to run an Excel macro from within python. 我想刷新许多excel文件中的所有数据库连接,但我不想从python中运行Excel宏。 I just wanted one line of xlwings code. 我只想要一行xlwings代码。

I looked everywhere on SO, github, other forums and blogs but couldn't find it. 我在SO,github,其他论坛和博客上到处都是,但是找不到。

My answer is below for others who'll have the same issue in the future. 我的答案是下面针对将来会遇到相同问题的其他人。

To RefreshAll connections in one excel file you only need: wbk.api.RefreshAll() 为了RefreshAll在一个Excel中的连接文件,你只需要: wbk.api.RefreshAll()

import xlwings as xw
# open Excel app in the background
app_excel = xw.App(visible = False)

wbk = xw.Book( 'D:\stuff\file.xlsx' )
wbk.api.RefreshAll()

# two options to save
wbk.save( 'D:\stuff\file.xlsx' ) # this will overwrite the file
wbk.save( 'D:\stuff\name1.xlsx' ) # this will save the file with a name

# kill Excel process
app_excel.kill()
del app_excel

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

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