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