使用 xlwings 在 excel 文件中刷新所有 [英] RefreshAll in excel file with xlwings

查看:312
本文介绍了使用 xlwings 在 excel 文件中刷新所有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在多个 excel 文件中RefreshAll 数据库连接,但我不想从 python 中运行 Excel 宏.我只想要一行 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.

我在 SO、github、其他论坛和博客上到处找,但都找不到.

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.

推荐答案

RefreshAll 连接在一个 excel 文件中你只需要:wbk.api.RefreshAll()

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

这篇关于使用 xlwings 在 excel 文件中刷新所有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆