import xlrd
def open_file(path):
# 打开并读取Excel文件
book = xlrd.open_workbook(path)
# 打印工作表数量
print(book.nsheets)
# 打印工作表名
print(book.sheet_names())
# 获取第一个工作表
first_sheet = book.sheet_by_index(0)
# 读取行
print(first_sheet.row_values(0))
# 读取单元格
cell = first_sheet.cell(0,0)
print(cell)
print(cell.value)
# 读取行切片
print(first_sheet.row_slice(rowx=0,start_colx=0,end_colx=3))
# --------------------
if __name__ == '__main__':
open_file(r'C:\test\sample.xls')
cells = first_sheet.row_slice(rowx=0, start_colx=0, end_colx=2)
for cell in cells:
print(cell.value)
联系客服