更新时间:2022-02-23 01:06:13
Pandas 文档说它使用 openpyxl 来处理 xlsx 文件.快速浏览 ExcelWriter
中的代码会发现类似这样的事情可能会奏效:
Pandas docs says it uses openpyxl for xlsx files. Quick look through the code in ExcelWriter
gives a clue that something like this might work out:
import pandas
from openpyxl import load_workbook
book = load_workbook('Masterfile.xlsx')
writer = pandas.ExcelWriter('Masterfile.xlsx', engine='openpyxl')
writer.book = book
## ExcelWriter for some reason uses writer.sheets to access the sheet.
## If you leave it empty it will not know that sheet Main is already there
## and will create a new sheet.
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
data_filtered.to_excel(writer, "Main", cols=['Diff1', 'Diff2'])
writer.save()