简体   繁体   English

附加到 Excel 中的工作表会创建一个新工作表而不是附加

[英]Appending to a sheet in Excel creates a new sheet instead of appending

I am trying to use this code to append a dataframe to an existing sheet in Excel, but instead of appending the new data to it, it creates a new sheet.我正在尝试将此代码用于 append 和 dataframe 到 Excel 中的现有工作表,但不是将新数据附加到它,而是创建一个新工作表。 Here is the code:这是代码:

import pandas as pd
import openpyxl as op

df = ['normal_dataframe']

with pd.ExcelWriter('test.xlsx', engine='openpyxl', mode='a') as writer:
    df.to_excel(writer, sheet_name='Sheet1', header=False, index=False)

'test.xlsx' has a 'Sheet1', but when the file is appended, theres 2 sheets. “test.xlsx”有一个“Sheet1”,但是当附加文件时,有 2 张。 'Sheet1' and 'Sheet11'. “Sheet1”和“Sheet11”。

One approach with COM: COM 的一种方法:

import win32com.client

xl = win32com.client.Dispatch("Excel.Application")

path = r'c:\Users\Alex20\Documents\test.xlsx'
wb = xl.Workbooks.Open(path)
ws = wb.Worksheets("Sheet1")
ws.Range("E9:F10").Value = [[9,9],[10,10]]
wb.Close(True)
xl.Quit()

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

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