簡體   English   中英

替換 excel 表中的數據

[英]Replace the data in an excel sheet

我有一個 10 張的 excel 文件,名為 A1、A2、... A10。 我想用新的 pandas dataframe 替換工作表 A2 的內容。 我沒有找到任何 function 進行這種轉換。

有沒有可用的解決方法?

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()

試試這個代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM