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设计】。

相关文章

Linux下python3.6.1环境配置教程

Linux下python3.6.1环境配置教程

linux系统环境自带python2.6,但有时我们项目使用的版本可能是3.x以上等等,此时我们需要在linux中再安装项目所需的python版本,此时就涉及多版本共存问题了,很多同学在...

python读取Android permission文件

python读取Android permission文件

今天用python解析一个文本文件,格式如下:复制代码 代码如下:[    {      &nb...

python排序方法实例分析

本文实例讲述了python排序方法。分享给大家供大家参考。具体如下: >>> def my_key1(x): ... return x % 10 ... >...

python自定义线程池控制线程数量的示例

1.自定义线程池 import threading import Queue import time queue = Queue.Queue() def put_data...

python多任务之协程的使用详解

1|0使用yield完成多任务 import time def test1(): while True: print("--1--") time.sleep(0.5)...