美文网首页
[python学习]xlrd 读Excel表

[python学习]xlrd 读Excel表

作者: 花小邪丶 | 来源:发表于2020-11-04 14:55 被阅读0次

    import xlrd

    path =r'D:\Desktop\T-投资计划.xls'

    wb = xlrd.open_workbook(path)# 打开文件

    #######################

    sh_names = wb.sheet_names()# 获取所有sheet名称

    sh_num = wb.nsheets# 返回Sheet的数目

    sh_lst = wb.sheets()# 返回所有Sheet对象的list

    print(sh_names)

    print(sh_num)

    print()

    sh1 = wb.sheet_by_index(0)# 通过索引获取sheet, == sh_lst[0]

    sh2 = wb.sheet_by_name('成长基金')# 通过索引获取sheet

    print(sh1.name, sh1.nrows, sh1.ncols)# sh1的名称,行数,列数

    print(sh2.name,  sh2.nrows, sh2.ncols)

    print()

    #######################

    # 获取整行和整列的值(数组)

    row_lst = sh1.row(1)# 获取指定行,返回Cell对象的list

    row_s = sh1.row_slice(1)# 获取指定行,返回Cell对象的list

    rows = sh1.row_values(1)# 获取第2行内容

    row_t = sh1.row_types(1)# 返回由该行中所有单元格的数据类型组成的列表

    row_len = sh1.row_len(1)# 返回该行的有效单元格长度

    col_lst = sh1.col(1)# 获取指定列,返回Cell对象的list

    cols = sh1.col_values(0)# 获取第1列内容

    print(row_lst)

    print(row_s)

    print(rows)

    print(row_t)

    print(row_len)

    print(col_lst)

    print(cols)

    print()

    # 获取单元格内容

    cell_obj = sh1.cell(1, 0)# 根据位置获取Cell对象

    cell_type = sh1.cell_type(1, 0)# 返回单元格中的数据类型

    cell_value = sh1.cell_value(1, 0)# 返回单元格中的数据

    print(cell_obj)

    print(cell_type)

    print(cell_value)

    cell_v = cell_obj.value# 返回单元格中的数据

    print(cell_v)

    相关文章

      网友评论

          本文标题:[python学习]xlrd 读Excel表

          本文链接:https://www.haomeiwen.com/subject/gesgvktx.html