[英]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.