把excel表拆成999数据的各个表
并且每个表的第一行都是一样的表头
import xlrd
import xlwt
import math
limit =999
readbook ='要读取的.xlsx'
saveBook ='要存的excel的路径/要存的.xlsx'
data = xlrd.open_workbook(readbook)
table = data.sheets()[0]
#行数
nrows = table.nrows
#列数
ncols = table.ncols
#表数量
sheets = math.ceil(nrows/limit)
workbook =xlwt.Workbook(encoding='ascii')
title = table.row_values(0)
# print("表头:",title)
for i in range(0,sheets):#每个表
worksheet = workbook.add_sheet(str(i))
for rowin range(0,limit):#表里的行数
if (row + limit + (limit-1)*(i-1) < nrows+1):#为了防止取不到数据报错
if (row ==0):#每个表的第一行都是表头
row_content = title
else:# 每个表的第一行都是从原来表的第二个起 1--> 1 下一个的1 -->999 在下一个1-> 999+998 在下一个 999+998 +998....
if i ==0:
rowOld = row
else:
rowOld = row + limit + (limit-1)*(i-1)-1
row_content = table.row_values(rowOld)
print("行数:", row,';表数:', i,";总行数:", rowOld)
for colin range(0,ncols):
worksheet.write(row,col,row_content[col])
workbook.save(saveBook)
或者拆成n个表
import xlrd
import xlwt
import math
limit =999
readbook ='netSignDetail.xlsx'
saveBook ='/Users/clairyang/PycharmProjects/untitled/test/netSignNew.xlsx'
saveRoad ='/Users/clairyang/PycharmProjects/untitled/test/netSignNew'
data = xlrd.open_workbook(readbook)
table = data.sheets()[0]
#行数
nrows = table.nrows
#列数
ncols = table.ncols
sheets = math.ceil(nrows/limit)
workbook =xlwt.Workbook(encoding='ascii')
title = table.row_values(0)
# print("表头:",title)
for i in range(0,sheets):#每个表
worksheet = workbook.add_sheet('表'+str(i+1))
for rowin range(0,limit):#表里的行数
if (row + limit + (limit-1)*(i-1) < nrows+1):
if (row ==0):#每个表的第一行都是表头
row_content = title
else:# 每个表的第一行都是从原来表的第二个起 1--> 2 下一个的1 -->999 在下一个1-> 999+998 在下一个 999+998 +998
if i ==0:
rowOld = row
else:
rowOld = row + limit + (limit-1)*(i-1)-1
row_content = table.row_values(rowOld)
# print("内容:",row_content)
print("行数:", row,';表数:', i,";总行数:", rowOld)
for colin range(0,ncols):
worksheet.write(row,col,row_content[col])
saveRoad_All = saveRoad +str(i) +'.xlsx'
workbook.save(saveRoad_All)
网友评论