简体   繁体   English

打开现有 Excel 文件、清除现有工作表并将数据框写入该工作表的最佳方法

[英]Best way to open existing excel file, clear an existing sheet and write dataframe to that sheet

Longwinded question, I know.冗长的问题,我知道。

But all solutions I have tried either delete all the sheets and replace them with one sheet containing my dataframe or breaks the excel file and requires excel to repair it.但是我尝试过的所有解决方案要么删除所有工作表并用一张包含我的数据框的工作表替换它们,要么破坏 excel 文件并需要 excel 来修复它。

Would be interested to know what the 'best' way to achieve this will be.有兴趣知道实现这一目标的“最佳”方式是什么。

Here is how i write data frame to excel file, after quick search I found here how u can delete certain sheet - https://www.pythonexcel.com/delete-sheet.php这是我如何将数据框写入 excel 文件,快速搜索后我在这里找到了如何删除某些工作表 - https://www.pythonexcel.com/delete-sheet.php

import pandas as pd from openpyxl import load_workbook从 openpyxl import load_workbook 导入大熊猫作为 pd

writer = pd.ExcelWriter(b, engine='xlsxwriter')
 df = pd.DataFrame({"q3": [""],
                    "q2": [""],
                     "q1": [""]})
        writer = pd.ExcelWriter(b, engine='xlsxwriter')
        df.to_excel(writer, sheet_name='Sheet1', index=False)
        
        writer.save()

` `

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

相关问题 Python Excel 在现有工作表中写入 DataFrame - Python Excel write a DataFrame in an existing sheet Python:将数据框写入到现有的Excel中,其中包含带有图像的工作表 - Python: Write a dataframe to an already existing excel which contains a sheet with images 如何在现有 Excel 工作表下方写入数据框而不会丢失数据透视表中的 excel 切片器? - How to write dataframe below an existing Excel sheet without losing slicer of excel in pivot table sheet? 如何在 .xlsm 文件中使用 Pandas 将数据框添加到现有 Excel 工作表 - How to add a dataframe to an existing Excel sheet with Pandas, on a .xlsm file 想要读取一个文件并使用 python 写入现有的 excel 表 - Want to read an a file and write to existing excel sheet using python Python:打开现有的Excel文件并计算工作表中的行数 - Python: open existing Excel file and count rows in sheet 无法将Pandas Dataframe附加到现有的Excel工作表 - unable to append pandas Dataframe to existing excel sheet 将DataFrame保存到现有Excel工作表的下一行 - Save DataFrame to next row in existing excel sheet 如何将数据框附加到现有的Excel工作表? - How to append a dataframe to existing excel sheet? 使用 Openpyxl 从特定列和行开始的现有工作表中写入现有的 excel 文件 - Write to an existing excel file using Openpyxl starting in existing sheet starting at a specific column and row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM