xlrd详解

# pip install xlrd==1.2.0
"""
    1.高版本的包不支持xlsx格式的文件,
    2.如果想操作xlsx请安装低版本的包
    这边 索引是从0开始的
"""

import xlrd

xls_file_src = "xls_demo.xls"
xlsx_file_src = "xlsx_demo.xlsx"


#############获取sheet名字###############
def get_sheet_names():
    wb = xlrd.open_workbook(xls_file_src)
    sheet_names = wb.sheet_names()
    print(sheet_names)


############最大值###############
def get_max_index():
    wb = xlrd.open_workbook(xlsx_file_src)
    sheeet_names = wb.sheet_names()
    for sheet_name in sheeet_names:
        sheet_obj = wb.sheet_by_name(sheet_name)
        max_rows = sheet_obj.nrows
        max_cols = sheet_obj.ncols
        print(f"sheet_name = {sheet_name} max_rows={max_rows} max_cols={max_cols}")


############获取所有的值###############
def get_cell_value():
    wb = xlrd.open_workbook(xlsx_file_src)
    sheeet_names = wb.sheet_names()
    for sheet_name in sheeet_names:
        sheet_obj = wb.sheet_by_name(sheet_name)
        max_rows = sheet_obj.nrows
        max_cols = sheet_obj.ncols
        for row_index in range(max_rows):
            for col_index in range(max_cols):
                value = sheet_obj.cell_value(row_index, col_index)
                print(value)

 

posted @ 2023-03-15 17:12    阅读(56)  评论(0编辑  收藏  举报