i have application write worksheet last column + 2 if there data , last column + 1 if worksheet empty. think empty worksheet follows:
from openpyxl.workbook.workbook import workbook book = workbook() sheet = book.active
when sheet.max_row
, sheet.max_column
, 1
both properties. zero, following:
if sheet.max_row == 1 , sheet.max_column == 1 , not sheet['a1'].value: start_col = 1 else: start_col = sheet.max_column + 2
having check value of cell seems bit overkill, not mention error-prone. expected following work:
if sheet.max_column == 0: start_col = 1 else: start_col = sheet.max_column + 2
is there reason max_row
, max_column
>= 1
? bug, intentional feature or reasonable side-effect of else? finally, there workaround (e.g. sheet.isempty()
).
by way, found following comment while browsing bug tracker: https://bitbucket.org/openpyxl/openpyxl/issues/514/cell-max_row-reports-higher-than-actual#comment-21091771:
rows empty cells still rows. might formatted future use or other stuff, don't know. 2.3 has improved heuristics still contain rows of empty cells.
this (and comment can no longer find link for) lead me believe first cell exists. in case, worth submitting feature request?
the reliable way determine if worksheet "empty" @ _cells
attribute. however, part of internal api , liable change. in fact change.
the max_row
, max_column
attributes used internally counters , must 1 or more.
a feature request without relevant code , tests rejected , i'm sceptical idea of "empty" worksheet in general.