当前位置: 移动技术网 > IT编程>脚本编程>Python > python合并已经存在的sheet数据到新sheet的方法

python合并已经存在的sheet数据到新sheet的方法

2019年01月09日  | 移动技术网IT编程  | 我要评论

鸿蒙钧天,朗拿甸度,六合彩88期

简单的合并,本例是横向合并,纵向合并可以自行调整。

import xlrd 

import xlwt
import shutil 
from xlutils.copy import copy 
import datetime 

# 打开要使用的excel,获取要需要写入的行数 
bk = xlrd.open_workbook('a.xlsx') #打开a文件
nbk = copy(bk)
newsh = nbk.add_sheet('totale') #新建total名字的sheet
nsheet = bk.nsheets
cur_col = 1
#scan all sheet in bk
print(nsheet)
for i in range(0, nsheet):
  sh = bk.sheet_by_index(i)
  print(sh.name)
  nrows = sh.nrows
  ncol = sh.ncols
  print(sh.nrows)
  print(sh.ncols)
  #scan all row in sh
  for j in range(0, ncol-1):
    for k in range(0, nrows-1):
      newsh.write(k,cur_col, label=sh.cell_value(k,j))
    cur_col = cur_col + 1
nbk.save('a-new.xls') #保存为a-new文件,其中包含了原始内容和新的total页

以上这篇python合并已经存在的sheet数据到新sheet的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网