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

2019-05-08 05:48发布

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)]

2条回答
疯言疯语
2楼-- · 2019-05-08 06:35

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.

查看更多
Ridiculous、
3楼-- · 2019-05-08 06:44

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.
    """
查看更多
登录 后发表回答