判断是否单元格是否为数字字母内容

bool(re.match(r'^[a-zA-Z0-9]+$', row[rownum + 1]))

判断行是否为float类型

type(row[rownum]) == float

获取workboot中sheet的名称

sheet.name 

场景读取某个文件夹下的excl并统计每个sheet页中存在满足条件的 记录数

import re
import os
import xlrd

def get_files_in_folder(folder_path):
    file_names = os.listdir(folder_path)
    return file_names
def readExcl(path):
    # 打开工作表
    workbook = xlrd.open_workbook(path)
    # 获取所有的工作薄
    sheets = workbook.sheet_names()
    # 获取sheet总个数
    num = workbook.nsheets
    countLine = 0
    # 返回的结果集
    for i in range(num):
        sheet = workbook.sheets()[i]
        # python3.8用法
        rows = sheet.nrows
        rownum = 0
        line = 0
        for i in range(rows):
            row = sheet.row_values(i)
            if rownum == 0:
                for td in range(len(row)):
                    if row[td] == "xx监测日期":
                        rownum = td
            elif type(row[rownum]) == float and bool(re.match(r'^[a-zA-Z0-9]+$', row[rownum + 1])): #条件判断
                line = line + 1
        if line > 0:
            print(sheet.name + " " + str(line))
            countLine = countLine+line
    return countLine

if __name__ == '__main__':
    path= "C:\\path\\"
    files = get_files_in_folder(path)
    cl = 0
    for file in files:
        cl = readExcl(path+file)+cl
    print("总数:"+str(cl))

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐