简体   繁体   English

python:将数据框更新到现有的excel工作表,而不会覆盖同一工作表和其他工作表上的内容

[英]python: update dataframe to existing excel sheet without overwriting contents on the same sheet and other sheets

Struggling for this for hours so I decided to ask for help from experts here: 奋斗了几个小时,所以我决定在这里寻求专家的帮助:

I want to modify existing excel sheet without overwriting content. 我想修改现有的Excel工作表而不覆盖内容。 I have other sheets in this excel file and I don't want to impact other sheets. 我在此excel文件中还有其他工作表,并且我不想影响其他工作表。

I've created sample code, not sure how to add the second sheet that I want to keep though. 我已经创建了示例代码,但是不确定如何添加我想保留的第二张表。

t=pd.date_range('2004-01-31', freq='M', periods=4)
first=pd.DataFrame({'A':[1,1,1,1],
             'B':[2,2,2,2]}, index=t)
first.index=first.index.strftime('%Y-%m-%d')
writer=pd.ExcelWriter('test.xlsx')
first.to_excel(writer, sheet_name='Here')
first.to_excel(writer, sheet_name='Keep')

#how to update the sheet'Here', cell A5:C6 with following without overwriting the rest?
#I want to keep the sheet "Keep"
update=pd.DataFrame({'A':[3,4],
                     'B':[4,5]}, index=pd.date_range('2004-04-30', 
                                                     periods=2,
                                                     freq='M'))

I've researched SO. 我已经研究过了。 But not sure how to write a dataframe into the sheet. 但不确定如何将数据框写入工作表。

Example I've tried: 我尝试过的示例:

import openpyxl
xfile = openpyxl.load_workbook('test.xlsx')
sheet = xfile.get_sheet_by_name('test')
sheet['B5']='wrote!!'
xfile.save('test2.xlsx')

Figured it out by myself: 我自己弄清楚了:

#Prepare the excel we want to write to
t=pd.date_range('2004-01-31', freq='M', periods=4)
first=pd.DataFrame({'A':[1,1,1,1],
             'B':[2,2,2,2]}, index=t)
first.index=first.index.strftime('%Y-%m-%d')
writer=pd.ExcelWriter('test.xlsx')
first.to_excel(writer, sheet_name='Here')
first.to_excel(writer, sheet_name='Keep')

#read the existing sheets so that openpyxl won't create a new one later
book = load_workbook('test.xlsx')
writer = pandas.ExcelWriter('test.xlsx', engine='openpyxl') 
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

#update without overwrites
update=pd.DataFrame({'A':[3,4],
                     'B':[4,5]}, index=(pd.date_range('2004-04-30', 
                                                     periods=2,
                                                     freq='M').strftime('%Y-%m-%d')))

update.to_excel(writer, "Here", startrow=1, startcol=2)

writer.save()

I'd suggest you update to the 2.4 (either the beta or a checkout) of openpyxl and use the built in support fro dataframes. 我建议您将openpyxl更新为2.4(测试版或签出版本),并使用内置的支持来回数据框。 These can now easily be converted by openypxl into rows that you do what you want with. 这些现在可以通过openypxl轻松转换为您要执行的操作。

See http://openpyxl.readthedocs.io/en/latest/pandas.html for details. 有关详细信息,请参见http://openpyxl.readthedocs.io/en/latest/pandas.html

暂无
暂无

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

相关问题 如何使用python2.7.14更新具有不同数据框的同一Excel工作表而不会覆盖excel文件中存在的先前数据? - How to update the same excel sheet with different dataframe without overwriting the previous data present in excel file using python2.7.14? 用 pandas dataframe 覆盖 excel 工作表而不影响其他工作表 - Overwrite an excel sheet with pandas dataframe without affecting other sheets Python:覆盖 Excel 文件中的现有工作表 - Python: overwriting an existing sheet in Excel file Python - Excel - 将工作表添加到现有工作簿而不删除工作表 - Python - Excel - Add sheet to existing workbook without removing sheets 如何使用python将数据从一个工作表添加到另一个工作簿中的现有工作表而不覆盖其他工作表的数据? - How to add data using python from one sheet to an existing sheet in another workbook without overwriting other sheet's data? Python Excel 在现有工作表中写入 DataFrame - Python Excel write a DataFrame in an existing sheet 使用python在现有Excel文件中的不同工作表中的相同Excel中的新工作表中合并结果摘要 - Consolidating Result Summary in new sheet in same excel from different sheets on a existing excel file using python Python Excelwriter 覆盖 excel 表 - Python Excelwriter overwriting excel sheet Python-如何防止for循环覆盖相同的Excel工作表 - Python - How to prevent for loop overwriting same excel sheet 从 pandas 到一张 excel 工作表的多个数据帧,而不用 python 删除其他工作表 - multiple dataframes from pandas to one excel sheet without deleting other sheets with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM