脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python合并已经存在的sheet数据到新sheet的方法

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

2021-04-28 00:18greenlaomao Python

今天小编就为大家分享一篇python合并已经存在的sheet数据到新sheet的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/greenlaomao/article/details/80483214

延伸 · 阅读

精彩推荐