![](/img/trans.png)
[英]How can I find the last non-empty column number of an Excel file using openpyxl?
[英]How can I find the last non-empty row of excel using openpyxl 3.03?
# Open file with openpyxl
to_be = load_workbook(FILENAME_xlsx)
s = to_be.active
last_empty_row = len(list(s.rows))
print(last_empty_row)
## Output: 13
s.rows
是一个生成器,它的列表包含每个行单元格的 arrays。
openpyxl
的 class Worksheet
具有属性max_rows
如果您正在使用 python 和 openpyxl 查找整个 xlsx 工作表的最后一个非空行。
尝试这个:
import openpyxl
def last_active_row():
workbook = openpyxl.load_workbook(input_file)
wp = workbook[sheet_name]
last_row = wp.max_row
last_col = wp.max_column
for i in range(last_row):
for j in range(last_col):
if wp.cell(last_row, last_col).value is None:
last_row -= 1
last_col -= 1
else:
print(wp.cell(last_row,last_col).value)
print("The Last active row is: ", (last_row+1)) # +1 for index 0
if __name__ = '___main__':
last_active_row()
这应该有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.