我发现 openpyxl 确实为已保存的文件报告了正确的 max_row 和 max_col 值,但是如果您操作工作表的内容并在保存之前需要这些值,问题仍然存在。
没有这样做的内置方法,因此您最好的选择是自己搜索行和列,最好通过从报告的值开始并向上和向左搜索来限制搜索。
工作表对象允许您单独访问行,但只能通过.itercols() 访问各个列。在一个循环中扫描所有列是否更快取决于您期望工作表的空白程度。
from openpyxl import load_workbook
wb = load_workbook('test.xlsx')
wb.worksheets[0]['h6'] = None
print((wb.worksheets[0].max_row, wb.worksheets[0].max_column))
def find_edges(sheet):
row = sheet.max_row
while row > 0:
cells = sheet[row]
if all([cell.value is None for cell in cells]):
row -= 1
else:
break
if row == 0:
return 0, 0
column = sheet.max_column
while column > 0:
cells = next(sheet.iter_cols(min_col=column, max_col=column, max_row=row))
if all([cell.value is None for cell in cells]):
column -= 1
else:
break
return row, column
print(find_edges(wb.worksheets[0]))
在此示例中,我加载了一个 Excel 工作表,其中包含您建议的数据,其值仍在 H6 中,已在第 3 行删除。
它首先打印openpyxl 报告的max_row 和max_column,然后使用工作表调用find_edges,以查找所需的实际值。
对于数据非常少的大型工作表,您可能希望在确定最后一行(以限制大小)后通过简单地迭代所有列来替换列扫描来测试速度,如下所示:
columns = sheet.iter_cols(max_row=row)
column = 1
ci = 1
while True:
try:
cells = next(columns)
if not all([cell.value is None for cell in cells]):
column = ci
ci += 1
except StopIteration:
break
但我希望第一种方法对于大多数有用的用例来说是最快的。
如果你更喜欢简短而不是可读:
def find_edges2(sheet):
def row():
for r in range(sheet.max_row, 0, -1):
if not all([cell.value is None for cell in sheet[r]]):
return r
row = row()
if not row:
return 0, 0
def column():
for c in range(sheet.max_column, 0, -1):
if not all([cell.value is None for cell in next(sheet.iter_cols(min_col=c, max_col=c, max_row=row))]):
return c
return row, column()