Source code

Download source archive ↓

src/openpyxl/worksheet/pagebreak.py

1# Copyright (c) 2010-2024 openpyxl
2
3from openpyxl.descriptors.serialisable import Serialisable
4from openpyxl.descriptors import (
5    Integer,
6    Bool,
7    Sequence,
8)
9
10
11class Break(Serialisable):
12
13    tagname = "brk"
14
15    id = Integer(allow_none=True)
16    min = Integer(allow_none=True)
17    max = Integer(allow_none=True)
18    man = Bool(allow_none=True)
19    pt = Bool(allow_none=True)
20
21    def __init__(self,
22                 id=0,
23                 min=0,
24                 max=16383,
25                 man=True,
26                 pt=None,
27                ):
28        self.id = id
29        self.min = min
30        self.max = max
31        self.man = man
32        self.pt = pt
33
34
35class RowBreak(Serialisable):
36
37    tagname = "rowBreaks"
38
39    count = Integer(allow_none=True)
40    manualBreakCount = Integer(allow_none=True)
41    brk = Sequence(expected_type=Break, allow_none=True)
42
43    __elements__ = ('brk',)
44    __attrs__ = ("count", "manualBreakCount",)
45
46    def __init__(self,
47                 count=None,
48                 manualBreakCount=None,
49                 brk=(),
50                ):
51        self.brk = brk
52
53
54    def __bool__(self):
55        return len(self.brk) > 0
56
57
58    def __len__(self):
59        return len(self.brk)
60
61
62    @property
63    def count(self):
64        return len(self)
65
66
67    @property
68    def manualBreakCount(self):
69        return len(self)
70
71
72    def append(self, brk=None):
73        """
74        Add a page break
75        """
76        vals = list(self.brk)
77        if not isinstance(brk, Break):
78            brk = Break(id=self.count+1)
79        vals.append(brk)
80        self.brk = vals
81
82
83PageBreak = RowBreak
84
85
86class ColBreak(RowBreak):
87
88    tagname = "colBreaks"
89
90    count = RowBreak.count
91    manualBreakCount = RowBreak.manualBreakCount
92    brk = RowBreak.brk
93
94    __attrs__ = RowBreak.__attrs__