python load_workbook如何获取每一行中非NONE 非空数据的单元格数量长度
时间: 2024-11-12 19:36:00 浏览: 9
在Python中,你可以使用`openpyxl`库的`load_workbook`函数加载Excel文件后,通过迭代每个单元格并检查其值是否为`None`或空字符串来获取非空数据的行数和单元格长度。这里是一个示例:
```python
from openpyxl import load_workbook
def count_non_empty_cells(worksheet):
non_empty_count = 0
for row in worksheet.iter_rows():
if all(cell.value is not None and cell.value != '' for cell in row):
non_empty_count += 1
return non_empty_count, len(worksheet.max_column)
workbook = load_workbook('your_file.xlsx')
worksheet = workbook['Sheet1'] # 如果有特定工作表,替换'Sheet1'
non_empty_row_count, max_col_length = count_non_empty_cells(worksheet)
print(f"Total rows with non-empty cells: {non_empty_row_count}")
print(f"Maximum column length (excluding empty cells): {max_col_length}")
# 记得关闭workbook
workbook.close()
阅读全文