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

yipeiwu_com6年前Python基础

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

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的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

关于Pytorch的MLP模块实现方式

关于Pytorch的MLP模块实现方式

MLP分类效果一般好于线性分类器,即将特征输入MLP中再经过softmax来进行分类。 具体实现为将原先线性分类模块: self.classifier = nn.Linear(con...

解析PyCharm Python运行权限问题

解析PyCharm Python运行权限问题

先通过 which python 获得 python 指令所在路径: $ which python /usr/bin/python 如上得到了其所在路径是 /usr/bin/pyt...

分享PyCharm的几个使用技巧

分享PyCharm的几个使用技巧

PyCharm是个十分强大的Python编辑器,笔者在日常的工作中学到了很多该IDE的使用技巧,有的是从别人那里学到的,有的是自己学习的。笔者深感自己的开发能力不足,因此希望能够将这些使...

python使用tensorflow深度学习识别验证码

本文介绍了python使用tensorflow深度学习识别验证码 ,分享给大家,具体如下: 除了传统的PIL包处理图片,然后用pytessert+OCR识别意外,还可以使用tessorf...

Python3 Post登录并且保存cookie登录其他页面的方法

如下所示: import urllib.request import sys import http.cookiejar import urllib.parse from bs4 i...