如何使用python计算每列excel中填充单元格的数量

时间:2022-03-17 11:10:33

I have a sheet in Excel which is not structured data as you can see here .

我在Excel中有一张表,它不是结构化数据,你可以在这里看到。

I tried using xlrd package

我尝试使用xlrd包

total_rows=worksheet.nrows
total_cols=worksheet.ncols

but this is giving me the maximum row count for a column. But I need filled cell count for each column. Could anyone suggest solution.

但这给了我一列的最大行数。但我需要为每列填充细胞计数。有谁能提出解决方案。

1 个解决方案

#1


3  

You can try to iterate through every row and check whether the cell is empty or not. You can use this:

您可以尝试遍历每一行并检查该单元格是否为空。你可以用这个:

import xlrd
book = xlrd.open_workbook('excel.xls')
sheet = book.sheet_by_index(0)

count = 0
for row in range(sheet.nrows):
    for col in sheet.row_values(row):
        if col.strip() != '':
            count += 1

#1


3  

You can try to iterate through every row and check whether the cell is empty or not. You can use this:

您可以尝试遍历每一行并检查该单元格是否为空。你可以用这个:

import xlrd
book = xlrd.open_workbook('excel.xls')
sheet = book.sheet_by_index(0)

count = 0
for row in range(sheet.nrows):
    for col in sheet.row_values(row):
        if col.strip() != '':
            count += 1