使用 xlrd - Python 读取范围内的行数

Read the number of rows in a range with xlrd - Python

我想读取一个范围内的行数。我有一个包含很多行的文件。我想知道列范围内的行数,比方说,从 A 列到 D 列。

workbook = xlrd.open_workbook(file)
sheet = workbook.sheet_by_index(0)
sheet.nrows[0:4] #I want to know the number of rows within that range, or "A{}:D{}"

如果有人知道这样做的方法,请帮助我。谢谢!

您可以遍历列... 使用循环:

for col_idx in range(0, num_cols):  # Iterate through columns 
        cell_obj = xl_sheet.cell(row_idx, col_idx)  # Get cell object by row, col

用xlrd没什么问题,我也是最开始用的。但是,我建议使用 pandas 库,因为它在整个 python 社区中使用得更多。

import pandas

dataframe = pandas.read_excel(file, usecols='A:D')
print(len(dataframe))