简体   繁体   中英

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. I just wanted one line of xlwings code.

I looked everywhere on SO, github, other forums and blogs but couldn't find it.

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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