天宫鹤

Python获取EXCEL实用行数

# 获取EXCEL实用行数
def get_excel_rows(_excel_path):
    """
    输入参数:
    :param _excel_path:Excel全路径
    功能:获取Excel实际行数,即培训人员数量
    """
    if not _excel_path.exists():
        print(f'{Fore.RED}文件不存在噢!!!\n{_excel_path}{Style.RESET_ALL}')
        return 0
    warnings.simplefilter(action='ignore', category=UserWarning)  # 忽略DataValidationError的警告
    try:
        df = pd.read_excel(_excel_path, sheet_name=0, header=None, skiprows=2)
        df = df.dropna(subset=[1])  # 删除第1列(姓名)中具有空值的行
        return df.shape[0]  # 返回Excel行数
    except ValueError as e:
        fun_name = inspect.currentframe().f_code.co_name  # 提取函数名称
        print(f'{Fore.RED}函数"{fun_name}"异常退出!!!{Style.RESET_ALL}')
        return 0

 

posted on 2024-07-18 11:42  GoGrid  阅读(2)  评论(0编辑  收藏  举报

导航