切割excel 文件

 

# -*- coding: utf-8 -*-
__author__ = '10043467'

import xlwt
import xlrd

srcChenjifileName = 'cj-x班.xls'
srcChenjiSheetName = '所有成绩'
classIdIndexInSrc = 5   #第一列是从0开始
classNum =14    #班级数
dstFileNamePrefix =''
dstFileNamePostfix = '班'

# classidList = ['1班','2班','3班', '4班','5班','6班','7班','8班','9班','10班','11班','12班','13班','14班'  ]
classidList = ['1','2','3', '4','5','6','7','8','9','10','11','12','13','14'  ]

def isRowBelongClass(row, sheetSrc ,classid ):
    xx = sheetSrc.cell_value(row,classIdIndexInSrc)
    if isinstance(xx,float):
        if int(xx) != classid:
            return False
        else:
            return  True
    else :
        if xx !=  classidList[classid -1]  :
            return False
        else:
            return True

def writeOneFile(filename, classid,sheetSrc):
    f = xlwt.Workbook()

    sheet1 = f.add_sheet('Sheet1',cell_overwrite_ok=True)
    writeRow =0
    for row in range(sheetSrc.nrows) :
        # xx = sheetSrc.cell_value(row,1)
        if isRowBelongClass(row,sheetSrc,classid) == False :
        # if int(xx) != classid:
            continue
        for col in range(sheetSrc.ncols):
            cellValue =sheetSrc.cell_value(row,col)
            sheet1.write(writeRow,col,cellValue)
        writeRow = writeRow +1
    f.save(dstFileNamePrefix + filename + dstFileNamePostfix + ".xls")

wb = xlrd.open_workbook(filename=srcChenjifileName)#打开文件
cjSheet = wb.sheet_by_name(srcChenjiSheetName)#通过名字获取表格

for classid in range(1,classNum +1):
    writeOneFile(str(classid), classid,cjSheet)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值