Source code

Download source archive ↓

src/openpyxl/styles/differential.py

1# Copyright (c) 2010-2024 openpyxl
2
3from openpyxl.descriptors import (
4    Typed,
5    Sequence,
6    Alias,
7)
8from openpyxl.descriptors.serialisable import Serialisable
9from openpyxl.styles import (
10    Font,
11    Fill,
12    Border,
13    Alignment,
14    Protection,
15    )
16from .numbers import NumberFormat
17
18
19class DifferentialStyle(Serialisable):
20
21    tagname = "dxf"
22
23    __elements__ = ("font", "numFmt", "fill", "alignment", "border", "protection")
24
25    font = Typed(expected_type=Font, allow_none=True)
26    numFmt = Typed(expected_type=NumberFormat, allow_none=True)
27    fill = Typed(expected_type=Fill, allow_none=True)
28    alignment = Typed(expected_type=Alignment, allow_none=True)
29    border = Typed(expected_type=Border, allow_none=True)
30    protection = Typed(expected_type=Protection, allow_none=True)
31
32    def __init__(self,
33                 font=None,
34                 numFmt=None,
35                 fill=None,
36                 alignment=None,
37                 border=None,
38                 protection=None,
39                 extLst=None,
40                ):
41        self.font = font
42        self.numFmt = numFmt
43        self.fill = fill
44        self.alignment = alignment
45        self.border = border
46        self.protection = protection
47        self.extLst = extLst
48
49
50class DifferentialStyleList(Serialisable):
51    """
52    Dedupable container for differential styles.
53    """
54
55    tagname = "dxfs"
56
57    dxf = Sequence(expected_type=DifferentialStyle)
58    styles = Alias("dxf")
59    __attrs__ = ("count",)
60
61
62    def __init__(self, dxf=(), count=None):
63        self.dxf = dxf
64
65
66    def append(self, dxf):
67        """
68        Check to see whether style already exists and append it if does not.
69        """
70        if not isinstance(dxf, DifferentialStyle):
71            raise TypeError('expected ' + str(DifferentialStyle))
72        if dxf in self.styles:
73            return
74        self.styles.append(dxf)
75
76
77    def add(self, dxf):
78        """
79        Add a differential style and return its index
80        """
81        self.append(dxf)
82        return self.styles.index(dxf)
83
84
85    def __bool__(self):
86        return bool(self.styles)
87
88
89    def __getitem__(self, idx):
90        return self.styles[idx]
91
92
93    @property
94    def count(self):
95        return len(self.dxf)