Remove Automatic Page Breaks in Openpyxl

Question:

I am using openpyxl in Python to write to a worksheet. I need to add page breaks to specific rows. I am able to successfully add those row breaks using this block of code:

page_break_rows = [44, 90, 135, 180, 226, 262]
    for row in page_break_rows:
        self.new_sheet.row_breaks.append(Break(id=row))

However, I cannot seem to get rid of the automatic page breaks that Excel creates, leaving me with a bunch of unnecessary pages on my worksheet. Any suggestions on how to either delete the page breaks that Excel makes or how to maybe move them to the right spots?

Asked By: WDW

||

Answers:

I had the same problem as yours. But it seems that I found a way to solve it. Please read the following lines:

import xlwings as xw
app=xw.App(visible=False,add_book=False)
workbook=app.books.open('XXX.xlsx')
worksheet=workbook.sheets('XXX')
worksheet.api.ResetAllPageBreaks() 
workbook.save()
app.quit()
Answered By: yMvcBeginner

The key line is as this: worksheet.api.ResetAllPageBreaks(), it applies the code from vba. Some of vba codes can be used with ‘api’ in xlwings.

Answered By: yMvcBeginner

Hey there after a lot of google I could not find a way to delete single pagebreak since pagebreaks doesn’t support del or remove or pop, but you can delete all pagebreaks by setting col.breaks to None.

Answered By: timreh