我有一个应用程序,如果已经有数据,则将工作表写入最后一列 + 2;如果工作表为空,则写入最后一列 + 1。我得到了我认为是空的工作表,如下所示:
from openpyxl.workbook.workbook import Workbook
book = Workbook()
sheet = book.active
当我做sheet.max_row
and sheet.max_column
, I get 1
对于这两个属性。我想得到零,所以我执行以下操作:
if sheet.max_row == 1 and sheet.max_column == 1 and not sheet['A1'].value:
start_col = 1
else:
start_col = sheet.max_column + 2
必须检查单元格的值似乎有点矫枉过正,更不用说容易出错了。我最初期望以下内容能够发挥作用:
if sheet.max_column == 0:
start_col = 1
else:
start_col = sheet.max_column + 2
有没有理由max_row
and max_column
总是>= 1
?这是一个错误、一个有意的功能还是其他东西的合理副作用?最后,是否有解决方法(例如sheet.isempty()
).
顺便说一句,我在浏览错误跟踪器时发现了以下评论:https://bitbucket.org/openpyxl/openpyxl/issues/514/cell-max_row-reports-higher-than-actual#comment-21091771 https://bitbucket.org/openpyxl/openpyxl/issues/514/cell-max_row-reports-higher-than-actual#comment-21091771:
具有空单元格的行仍然是行。它们可能会被格式化以供将来使用或其他用途,我们不知道。 2.3 稍微改进了启发式,但仍包含空单元格行。
这(以及我无法再找到链接的另一条评论)让我相信第一个单元实际上总是存在。在这种情况下,是否值得提交功能请求?