Skip to content
Advertisement

Remove Automatic Page Breaks in Openpyxl

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?

Advertisement

Answer

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()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement