我正在尝试使用两种不同的方法将列表追加到现有工作表(将列表作为一行添加)。第一个运行正常,但表中没有任何变化。第二个追加了列表,但添加了新的表格(这不是我的目标)。现有工作表的问题在于,前面有A5列中的数据,我想附加填充空白单元格(A1至A4)的列表。换句话说,我不想删除现有数据或通过包含nwe行来移动它。我已经尝试了以下方法:
#the existing sheet looks like that:
A1 A2 A3 A4 A5 A6......
5 6 ......
2 7 ......
. . ......
. . ......
import pandas as pd
import numpy as np
import openpyxl
#solution 1
lista=[1,2,3,4]
wb= openpyxl.load_workbook('teste.xlsx')
sheetname = wb['Hoja1']
sheetname.cell(row=3,column=1).value=str(lista)
#solution 2
with pd.ExcelWriter('teste.xlsx', mode='a') as writer:
pd.DataFrame(lista).to_excel(writer, header=False, index=False, startrow=3, startcol=1,
sheet_name="Hoja1")
你能帮我吗?
You can use
pandas.to_excel
andpandas.read_excel
(https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_excel.html and https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html), the data you can add to the dataframe like columns, not rows: