【问题标题】:Exporting dataframe to existing Excel sheet in workbook breaks pivot slicer connection in other sheets将数据框导出到工作簿中的现有 Excel 工作表会破坏其他工作表中的数据透视切片器连接
【发布时间】:2021-10-21 11:27:01
【问题描述】:

我的代码的工作方式与我希望的完全一样,从 df 获取数据并将其插入到所需的 Excel 文件中,同时跳过相应的行。但是,当我点击 .save() 函数时,其他引用数据的工作表(主要通过枢轴)似乎会中断,即使作者没有触及它们。我可以将数据插入另一个 Excel 文件,复制并粘贴与 python 数据放置的完全相同的数据,并且相应的工作表不会中断,但会显示正确的信息。当 Python 写入文件时,如何阻止其他工作表损坏?

filename_in = 'File Location In'
filename_out = 'File Location Out'
sheet_name = 'Detail'
pos_detail_data_df.to_excel(filename_in, sheet_name=sheet_name, header = False, index = False)
df = pd.read_excel(filename_in, sheet_name=sheet_name)
book = load_workbook(filename_in)
writer = pd.ExcelWriter(filename_out, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
writer.sheets
df.to_excel(writer, sheet_name, index=False, startrow = 2, header = False)
writer.save()

编辑: 代码已更新以反映来自下方的帮助。但是,现在该过程将简单地从我的 filename_out 中删除所有内容,并仅将其替换为 filename_in 中的工作表

【问题讨论】:

  • df 在哪里使用?它已创建,但随后出现 pos_detail_data_df.to_excel(writer。 “好像坏了”是什么意思?
  • @MDR 我对 Python 还是比较陌生,这只是包含在我正在使用的原始编码中。 “中断”是指当我插入数据时,第一张表(未写入)不再维护其切片器或字段设置中的“重复项”。
  • 代码看起来不完整。如果您调整工作簿中其他地方引用的工作表,则必须自己更新引用。
  • @CharlieClark 仅供参考:评论我的回答是它是一个切片器扩展。我相信从 2020 年 10 月起,它们不受 groups.google.com/g/openpyxl-users/c/TKnnFMISB0c 的支持。
  • 我不认为切片器与任何破损有关。 Openpyxl 将简单地删除它们,但根据下面的答案,它本身不应该破坏任何东西。

标签: python excel pandas openpyxl xlsx


【解决方案1】:

我找到了一个带有切片器的 Excel 文件,所以我看了一下。

示例文件:

试试:

import pandas as pd
from openpyxl import load_workbook

# sample Excel file with slicers.
# if required download and unzip and put in the folder with this script
sample_file = 'https://www.contextures.com/pivotsamples/regionsalesslicer.zip'

# set your filename_in, filename_out, and sheet_name
filename_in = 'regionsalesslicer.xlsx'
filename_out = 'regionsalesslicerUpdated.xlsx'
sheet_name = 'Sales Data'

# read in the Excel file with pd.read_excel rather than pd.ExcelFile
# just to play safe and avoid any BadZipFile: File is not a zip file errors
df = pd.read_excel(filename_in, sheet_name=sheet_name)



##################  WHATEVER YOU WANT BELOW UNTIL LINE 37 ##################

# check the contents
print(df.head(2), '\n')

# make a change (or changes) to your df
# in the case just swap 'Carrot' for 'Orange' in the 'Product' column
df.loc[df['Product'] == 'Carrot', 'Product'] = 'Orange'

# check the contents after the change
print(df.head(2), '\n')

# as long as you have imported from the top two lines and read the file
# and not called ExcelWriter before this point all the other lines above
# are up to you.

##################  WHATEVER YOU NEED ABOVE AFTER LINE 15 ##################



# from this point on try...
book = load_workbook(filename_in)
writer = pd.ExcelWriter(filename_out, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

df.to_excel(writer, sheet_name, index=False)

writer.save()

在生成的文件中(在上面的示例代码中,我们使用了filename_out = 'regionsalesslicerUpdated.xlsx'),切片器仍然有效。

示例:

显示“橙色”。让我们刷新数据...


切片器/过滤器显示“橙色”...

从 pandas 导出到 Excel 并没有删除任何工作表等...

我们已成功将数据框覆盖到 Excel 中的现有工作表中。

【讨论】:

  • 当我尝试这个时,它只用来自 filename_in 的工作表覆盖 filename_out
  • 对于哪个文件?如果您尚未使用测试文件尝试上述代码,请执行此操作。下载测试文件并按原样使用上面的代码。看看会产生什么。
  • 我已经意识到这个问题,它不仅仅是一个切片器,它是一个显然不受支持的切片器扩展。你知道切片机EXTENTION的解决方法吗?
  • 感谢您找到该内容,我尝试查找该内容,但在搜索时找不到类似内容。由于我对 Python 还很陌生,你用谷歌搜索什么来得到这个答案?
猜你喜欢
  • 2020-03-27
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-22
相关资源
最近更新 更多