简体   繁体   English

熊猫-将Excel文件保存回/覆盖现有工作表

[英]Pandas - Saving Excel File back into/overwriting an existing sheet

with my code below I am trying to open existing excel file (wos.xlsx) and then overwrite the "Detail - All" sheet with another dataframe I have saved called 'results'. 使用下面的代码,我试图打开现有的excel文件(wos.xlsx),然后用我保存的另一个名为“结果”的数据框覆盖“详细信息-全部”表。 But what saves in my path is the excel file "wos.xlsx" , just with 1 new tab "Detail-All", and none of the other tabs (there were 7). 但是保存在我的路径中的是excel文件“ wos.xlsx”,仅带有1个新选项卡“ Detail-All”,其他任何选项卡都没有(共有7个)。 My goal was just to replace the "Detail-All" tab with my 'results' dataframe. 我的目标只是用“结果”数据框替换“全部详细信息”标签。 Not sure where I went wrong. 不知道我哪里做错了。

ws_dict = pd.read_excel('wos.xlsx',
                        sheetname=None)

ws_dict['Detail - All'] = results
print(ws_dict)

with pd.ExcelWriter('wos.xlsx',
                    engine='xlsxwriter') as writer:

    for ws_name, df_sheet in ws_dict.items():
        results.to_excel(writer, sheet_name='Detail - All')

This is because you are overwriting the excel file 'wos.xlsx' with xlsxwriter, deleting any other sheet that was there before. 这是因为您正在用xlsxwriter覆盖excel文件'wos.xlsx',从而删除了之前存在的任何其他工作表。 If you want to edit instead of overwriting, you should use xlwings instead. 如果要编辑而不是覆盖,则应改用xlwings。

Example (not tested): 示例(未经测试):

import xlwings as x
wb = x.Book('wos.xlsx')
wb.sheets['Detail - All'].range('A1').options(index=False, header=True).value = results

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:覆盖 Excel 文件中的现有工作表 - Python: overwriting an existing sheet in Excel file 如何使用熊猫将工作表添加到现有的Excel文件中? - How to add sheet to existing excel file with pandas? 如何在不覆盖数据的情况下写入现有的 excel 文件(使用熊猫)? - How to write to an existing excel file without overwriting data (using pandas)? pandas 写入 excel 覆盖现有的 excel 行 - pandas write to excel overwriting the existing excel rows 如何通过使用 pandas 在工作表中覆盖来在现有 excel 中写入 json 数据? - How can I write json data in an existing excel by overwriting in a sheet using pandas? 熊猫:如何在同一工作表的现有xlsx文件中写入数据而不会覆盖旧数据 - pandas: how to write data in a existing xlsx file in the same sheet without overwriting the old data 如何使用 Pandas 在现有 excel 文件中保存新工作表? - How to save a new sheet in an existing excel file, using Pandas? 如何在 .xlsm 文件中使用 Pandas 将数据框添加到现有 Excel 工作表 - How to add a dataframe to an existing Excel sheet with Pandas, on a .xlsm file 将 Pandas 数据集保存到 csvs 而不覆盖现有文件 - Saving pandas datasets to csvs without overwriting existing files Python Pandas:Output 到 ZBF57C906FA7D2BB66D07372E41585Dls9 覆盖选定的工作表。 - Python Pandas: Output to excel “.xls” spreadsheet with just overwriting the selected sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM