![](/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.