当前位置:   article > 正文

python 的openpyxl对两个表的横向,竖向合并_python合并excel出现横

python合并excel出现横

列合并,即横向合并 

 

  1. from openpyxl import Workbook,load_workbook
  2. wb=Workbook()
  3. wb=load_workbook("excel.xlsx")
  4. wslist=wb.worksheets
  5. ws1=wslist[0]
  6. ws2=wslist[1]
  7. ws3=wslist[2]
  8. Columns=ws1.columns
  9. data1 = ws1["A1":"C3"]
  10. data2 = ws2["A1":"C3"]
  11. for i in range(len(data1)):
  12. for j in range(len(data1[i])):
  13. ws3.cell(i+1, j+1, data1[i][j].value)
  14. n = len(data1[0])
  15. for i in range(len(data2)):
  16. for j in range(len(data2[i])):
  17. ws3.cell(i+1, j+n+1, data2[i][j].value)
  18. wb.save("excel3.xlsx")

 

 行合并,即放在下面的那种

  1. from openpyxl import Workbook,load_workbook
  2. wb=Workbook()
  3. # wb.save("excel.xlsx")
  4. wb=load_workbook("excel.xlsx")
  5. # print(wb.sheetnames)
  6. # ws=wb.active
  7. # ws=wb.sheetnames
  8. wslist=wb.worksheets
  9. ws1=wslist[0]
  10. ws2=wslist[1]
  11. ws3=wslist[2]
  12. # ws3.cell(1,1,21)
  13. #wb.save("excel1.xlsx")
  14. rows=ws1.rows
  15. # #print(rows)
  16. # for row in rows:
  17. # for cell in row:
  18. # print(cell.value)
  19. '''合并表格'''
  20. data1=ws1["A1:C3"]
  21. data2=ws2["A1:C3"]
  22. data=data1+data2
  23. print(data)
  24. for i in range(len(data)):
  25. for j in range(len(data[i])):
  26. print(data[i][j].value)
  27. ws3.cell(i+1,j+1,data[i][j].value)
  28. wb.save("excel2.xlsx")

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/145062?site
推荐阅读
相关标签
  

闽ICP备14008679号