Add possibility to insert page breaks after columns

Issue #998 resolved
Jürnjakob Dugge created an issue

At the moment, it's possible to insert manual page breaks using the page_breaks attribute. There are always interpreted as row breaks (implemented in 7d095467ff12).

It would be useful to be able to specify page breaks for columns as well (for documents wider than one page).

Comments (6)

  1. CharlieC

    The simple answer is I don't know much about breaks but overwriting the tagname is always possible.

  2. Jürnjakob Dugge reporter

    Ah yes, that works! Simply setting openpyxl.worksheet.pagebreak.PageBreak.tagname = 'colBreaks' causes Breaks to be interpreted as page breaks after columns.

    from openpyxl import Workbook
    from openpyxl.compat import range
    from openpyxl.utils import get_column_letter
    from openpyxl.worksheet.pagebreak import Break
    
    wb = Workbook()
    dest_filename = 'empty_book.xlsx'
    
    ws = wb.active
    for row in range(1, 20):
        for col in range(1,30):
            _ = ws.cell(column=col, row=row, value="{0}".format(get_column_letter(col)))
    
    openpyxl.worksheet.pagebreak.PageBreak.tagname = 'colBreaks'  # Change tag from rowBreaks to colBreaks
    ws.page_breaks.append(Break(id=5))  # Page break after column 5
    ws.page_breaks.append(Break(id=7))  # Page break after column 7
    
    wb.save(filename = dest_filename)
    

    It's not possible to specify page breaks for rows and columns, but for our application, this is perfectly acceptable. Thank you!

  3. Log in to comment