现有一个excel表格:
import pandas as pd
import openpyxl
df1 = pd.DataFrame(
{
"num1": [21,25,56,11],
"num2": [31,35,36,12],
"num3": [41,45,46,14],
},
index=[0, 1, 2,3],
)
df1
#用openpyxl打开excel
wb=openpyxl.load_workbook('C:\\Users\\19051\\Desktop\\test1.xlsx')
#打开指定的Sheet
ws = wb['Sheet1']
startCol = 3
#下面两行的意思是,将df1的每一行转成列表
for i in range(0, df1.shape[0]):
eachRowList = df1.iloc[i,:].tolist()
#取每个列表里面的值
for j in range(0,len(eachRowList)):
#row 代表从几行开始, columns 代表从第几列开始
#这里是从第6行, 第3列开始插入
ws.cell(row = i+6, column = startCol+j).value =eachRowList[j]
#保存为新的表格
wb.save('C:\\Users\\19051\\Desktop\\test2.xlsx')