使用python刷新Excel外部数据连接

2024-09-29 23:21:59 发布

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

我有一个外部数据连接的excel文件。我需要使用python刷新连接数据。在

我试过苏打

import win32com.client
import os
fileName="testconn.xlsx"
xl = win32com.client.DispatchEx("Excel.Application")
wb = xl.workbooks.open(fileName)
xl.Visible = True
wb.RefreshAll()
wb.Save()
xl.Quit()

但是这个解决方案需要在机器上安装excel。在

我认为另一种方法是: -如果我知道如何获得数据连接的URL和加载它们的命名范围的映射,我可以从URL下载数据并使用openpyxl更新命名范围中的数据。在

有更好的方法吗?任何python库都有检索连接和刷新连接的功能吗?在

提前感谢:)


Tags: 文件数据方法importclienturlosfilename
1条回答
网友
1楼 · 发布于 2024-09-29 23:21:59

刷新Excel工作表肯定需要安装Excel。所以你的方法似乎很好,尽管可能有更简单的方法。Pandas有一个很好的库来操作Excel电子表格pandas.read_excelpandas.DataFrame.to_excel函数http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.htmlhttp://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html但仅使用Pandas无法写入命名区域,它至少需要指定工作表和起始单元格(如果命名区域不更改起始单元格/工作表,则很好)。{可以在此处使用另一个名为^的包来添加一个名为^的包: http://xlsxwriter.readthedocs.io/example_defined_name.html#ex-defined-namehttps://xlsxwriter.readthedocs.io/working_with_pandas.html

import xlsxwriter
workbook = xlsxwriter.Workbook('defined_range.xlsx')
worksheet.write('defined_range', data) # writes to defined_range in the Excel workbook 

Pandas还允许您直接从URL读取数据,这将有助于您提出的解决方案pandas.read_htmlhttp://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_html.html

相关问题 更多 >

    热门问题