WorkSheet.page_breaks property uninitialized when reading worksheet without breaks
Issue #1368
resolved
When reading in an existing workbook containing a sheet without page breaks the ws.page_breaks property is an empty list.
Due to this, any breaks appended to ws.row_breaks or ws.col_breaks are ignored when saving the file due to Serialization relying on the tuple that should be in ws.page_breaks.
This reproduces the bug in 3.0.0, but I suspect the bug has existed since commit 2a0fcc9
import openpyxl as opx from openpyxl.worksheet.pagebreak import Break wb = opx.load_workbook(filename = 'SomeWorkbookWithaSheet1woutPageBreaks.xlsx') ws = wb['Sheet1'] # verify page_breaks is empty list and not 2 val tuple print(f'Collections in page_breaks: {len(ws.page_breaks)}') # add a break, it'll be ignored anyway ws.row_breaks.append(Break(id=5)) # Show there is a break there print(f'Breaks in row_breaks: {ws.row_breaks.count}') # Serializer ignores it wb.save('NewWorkbookAlsowoutPageBreaks.xlsx') # Rename the above to zip, extract, view ./xl/worksheets/sheet1.xml # and note the lack of a <rowBreaks /> element
This appears to be due to openpyxl/openpyxl/worksheet/_reader.py instantiating page_breaks as [], then relying on deserialization to find a rowBreaks or colBreaks element so a break collection can be added.
Comments (2)
-
-
- changed status to resolved
Handle rowBreak and colBreak more explicitly and demote ws.page_breaks to a property. Resolves
#1368→ <<cset ebb43eeed3fb>>
- Log in to comment
Thanks for the report. I think this might have something to do with the “legacy” API conflicting with how openpyxl actually stores stuff.