RefreshAll in excel file with xlwings

倖福魔咒の 提交于 2020-06-26 12:54:18

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/53871709/refreshall-in-excel-file-with-xlwings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!