简体   繁体   English

替换 excel 表中的数据

[英]Replace the data in an excel sheet

I have an excel file of 10 sheets named like A1, A2, ... A10.我有一个 10 张的 excel 文件,名为 A1、A2、... A10。 I would like to replace the contents of sheet A2 by a new pandas dataframe.我想用新的 pandas dataframe 替换工作表 A2 的内容。 I dont find any function for such a transformation.我没有找到任何 function 进行这种转换。

Is there any workaround available for this?有没有可用的解决方法?

import pandas
from openpyxl import load_workbook

df = pandas.DataFrame() # your dataframe

book = load_workbook('your_excel')
writer = pandas.ExcelWriter('your_excel', engine='openpyxl')
writer.book = book

idx=book.sheetnames.index('A2')
book.remove(book.worksheets[idx])
book.create_sheet('A2',idx)

writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

df.to_excel(writer, "A2",index=0,startrow=0,startcol=0)
writer.save()

Try this code试试这个代码

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM