问题使用python刷新excel数据透视表

问题描述 投票:0回答:1

我正在尝试使用 openpyxl 库刷新 Excel 数据透视表。

数据表

结果表

如果我更新数据表中的值,则只有当我重新打开 Excel 文件时,结果表中的数据透视表才会更新。我无法获取 python 代码的 new_df 变量中的更新值。 我需要在进一步的代码中使用 new_df 变量。因此,重新打开文件来刷新数据对我没有帮助。

还有其他解决方法可以解决我在 Linux 机器中的问题吗?

这是代码

from openpyxl import load_workbook
import pandas as pd

wb = load_workbook("test_excel_file2.xlsx")
ws = wb["result"]
pivot= ws._pivots[0]
pivot.cache.enableRefresh = True
pivot.cache.refreshOnLoad = True
wb.save(excel_filepath)

new_df = pd.read_excel("test_excel_file2.xlsx", sheet_name=calc_sheet)
print(new_df)
python python-3.x pandas openpyxl
1个回答
0
投票

没有办法做到这一点 - openpyxl 只是编辑 Excel 文件,所有计算都由 Excel 本身完成。不过,您可以自动启动/关闭 Excel,以便它重新计算公式或在 Python 脚本中进行计算并使用 openpyxl 编辑所需的单元格。

© www.soinside.com 2019 - 2024. All rights reserved.