列合并,即横向合并
from openpyxl import Workbook,load_workbook
wb=Workbook()
wb=load_workbook("excel.xlsx")
wslist=wb.worksheets
ws1=wslist[0]
ws2=wslist[1]
ws3=wslist[2]
Columns=ws1.columns
data1 = ws1["A1":"C3"]
data2 = ws2["A1":"C3"]
for i in range(len(data1)):
for j in range(len(data1[i])):
ws3.cell(i+1, j+1, data1[i][j].value)
n = len(data1[0])
for i in range(len(data2)):
for j in range(len(data2[i])):
ws3.cell(i+1, j+n+1, data2[i][j].value)
wb.save("excel3.xlsx")
行合并,即放在下面的那种
from openpyxl import Workbook,load_workbook
wb=Workbook()
# wb.save("excel.xlsx")
wb=load_workbook("excel.xlsx")
# print(wb.sheetnames)
# ws=wb.active
# ws=wb.sheetnames
wslist=wb.worksheets
ws1=wslist[0]
ws2=wslist[1]
ws3=wslist[2]
# ws3.cell(1,1,21)
#wb.save("excel1.xlsx")
rows=ws1.rows
# #print(rows)
# for row in rows:
# for cell in row:
# print(cell.value)
'''合并表格'''
data1=ws1["A1:C3"]
data2=ws2["A1:C3"]
data=data1+data2
print(data)
for i in range(len(data)):
for j in range(len(data[i])):
print(data[i][j].value)
ws3.cell(i+1,j+1,data[i][j].value)
wb.save("excel2.xlsx")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)