根据excel列中是否有string 类型的值来统计行数,列中如果是行数,则不统计在内。
#import package
import pandas as pd
from openpyxl import load_workbook
from openpyxl import Workbook
import os
#将excel 表名,页签名,每页最大行数记录到列表中lis,再将lis这个列表放入final_data列表中
def getrownum_allsheet(table,final_data,column_num,exc_string):
wb = load_workbook(table)
# 遍历循环所有sheet
sheets = wb.sheetnames
print(sheets)
for i in range(1, len(sheets)):
lis = []
max_row = 0
sheet = wb[wb.sheetnames[i]]
# 获取晚间的最大行数
print(sheet.max_row)
for j in range(1, sheet.max_row+1, 1): # 从第一行开始到最后一行逐行进行
print(sheet.cell(row=j, column=column_num).value)
act_string = sheet.cell(row=j, column=column_num).value
# if isinstance(act_string, str) and act_string.upper() == exc_string: # 如果该行第column_num列值为'exc_string'
if isinstance(act_string, str) and len(act_string) != 0: #判断 act_string的值是否为STRING类型
max_row += 1
print(max_row)
lis.append(table)
lis.append(sheet.title)
lis.append(max_row)
final_data.append(lis)
#新建一个excel,把final_data列表中的数据写入到这个excel中
def record_tablename_sheetname_rownum(final_data):
# 新建一个Excel文档,用于存储数据
new_wb = Workbook()
sheet = new_wb.active
# 为sheet命名
sheet.title = "FinalData"
# 为sheet表添加标题行
sheet.append(["文件名", "sheetName", "行数"])
for row in final_data:
sheet.append(row)
new_wb.save(filename='Result1.xlsx')
#get file dir
# path = 'C:\\Scripts\\Test Cases'
path = path = os.getcwd()
exc_string = 'PASS'
column_num = 4
# got all .xlsx files
tables = []
file_list = os.walk(path)
for fpath, dirs, files in file_list:
for i in files:
if i.endswith(".xlsx"):
#将路径+excel名称组合后放入table列表中
# tables.append(os.path.join(fpath,i))
# print(os.path.join(fpath,i))
#将excel名称放入table列表中
tables.append(i)
# print(tables)
#组织数据:一列是文件名,一列是行数
final_data = []
for table in tables:
getrownum_allsheet(table,final_data,column_num,exc_string)
record_tablename_sheetname_rownum(final_data)