python读取xls表格中指定列或行范围的数据

import xlrd

# 打开Excel文件
workbook = xlrd.open_workbook('test01.xls')
# 获取第一个工作表
worksheet = workbook.sheet_by_index(0)

# 指定的行区域
# 读取第(row_index_x+1)行中,第(start_cols+1)列至第end_cols列范围的数据
start_cols = 0  # 第(start_cols+1)列
end_cols = 3  # 第end_cols列
row_index_x = 0  # 第(row_index_x+1)行
data1 = worksheet.row_values(row_index_x, start_cols, end_cols)
print("读取第" + str(row_index_x + 1) + "行中的," + "" + str(start_cols + 1) + "列至" + str(end_cols) + "列的数据为:", data1)

# 指定的列区域
# 读取第(column_index_x+1)列中,第(start_rows+1)行至第end_rows行范围的数据
start_rows = 0  # 第(start_rows+1)行
end_rows = 4  # 第end_rows行
column_index_x = 1  # 第(column_index_x+1)列
data2 = worksheet.col_values(column_index_x, start_rows, end_rows)
print("读取第" + str(column_index_x + 1) + "列中的," + "" + str(start_rows + 1) + "行至" + str(end_rows) + "行的数据为:", data2)

 

执行结果:

读取第1行中的,第1列至3列的数据为: ['姓名', '年龄', '电话']
读取第2列中的,第1行至4行的数据为: ['年龄', 23.0, 13.0, 45.0]

 

posted @ 2024-04-25 11:43  Doraaaaaaaaaa  阅读(42)  评论(0编辑  收藏  举报