简体   繁体   中英

xlwt - How to add page breaks to an Excel File?

I'm trying to add page breaks whenever a column's value changes in Excel using Python and xlwt .

Does anyone know how to do this?

I found one example but they don't really say if their code works, and they don't say what the numbers mean in the tuple:

ws.horz_page_breaks = [(54, 0, 255), (108, 0, 255)]

Doing some web research I found this OpenOffice.org document describing Excel's format and BIFF records. It seems for horizontal breaks (pag. 181), each tuple represents:

  • Index to first row BELOW the page break
  • Index to first column of the page break
  • Index to last column of the page break

So, for the example you show in the question, you have two page breaks, one over row 54 and another one over row 108, both of them spanning from column 0 to column 255.

Same applies for vertical breaks; just swap "row" and "column" in the previous description.

Digging in xlwt's source code, it turns out {vert,horiz}_page_breaks are properties (see Worksheet.py in the source distribution) that end up being passed to BIFFRecords.{Vertical,Horizontal}PageBreaksRecord (see BIFFRecords.py ). Those last two classes are documented. Here's the docs for them, in case you find them useful:

class HorizontalPageBreaksRecord(BiffRecord):
    """  
    This  record  is  part  of  the  Page  Settings  Block. It contains all
    horizontal manual page breaks.

    Record HORIZONTALPAGEBREAKS, BIFF8:
    Offset  Size  Contents
    0       2     Number of following row index structures (nm)
    2       6nm   List of nm row index structures. Each row index
                  structure contains:
                    Offset  Size    Contents
                    0       2       Index to first row below the page break
                    2       2       Index to first column of this page break
                    4       2       Index to last column of this page break

    The row indexes in the lists must be ordered ascending.
    If in BIFF8 a row contains several page breaks, they must be ordered
    ascending by start column index.
    """

class VerticalPageBreaksRecord(BiffRecord):
    """  
    This  record  is  part  of  the  Page  Settings  Block. It contains all
    vertical manual page breaks.

    Record VERTICALPAGEBREAKS, BIFF8:
    Offset  Size  Contents
    0       2     Number of following column index structures (nm)
    2       6nm   List of nm column index structures. Each column index
                  structure contains:
                    Offset  Size    Contents
                    0       2       Index to first column following the page
                                    break
                    2       2       Index to first row of this page break
                    4       2       Index to last row of this page break

    The column indexes in the lists must be ordered ascending.
    If in BIFF8 a column contains several page breaks, they must be ordered
    ascending by start row index.
    """

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM