Source code

Download source archive ↓

src/openpyxl/styles/table.py

1# Copyright (c) 2010-2024 openpyxl
2
3from openpyxl.descriptors.serialisable import Serialisable
4from openpyxl.descriptors import (
5    Typed,
6    Float,
7    Bool,
8    Set,
9    Integer,
10    NoneSet,
11    String,
12    Sequence
13)
14
15from .colors import Color
16
17
18class TableStyleElement(Serialisable):
19
20    tagname = "tableStyleElement"
21
22    type = Set(values=(['wholeTable', 'headerRow', 'totalRow', 'firstColumn',
23                        'lastColumn', 'firstRowStripe', 'secondRowStripe', 'firstColumnStripe',
24                        'secondColumnStripe', 'firstHeaderCell', 'lastHeaderCell',
25                        'firstTotalCell', 'lastTotalCell', 'firstSubtotalColumn',
26                        'secondSubtotalColumn', 'thirdSubtotalColumn', 'firstSubtotalRow',
27                        'secondSubtotalRow', 'thirdSubtotalRow', 'blankRow',
28                        'firstColumnSubheading', 'secondColumnSubheading',
29                        'thirdColumnSubheading', 'firstRowSubheading', 'secondRowSubheading',
30                        'thirdRowSubheading', 'pageFieldLabels', 'pageFieldValues']))
31    size = Integer(allow_none=True)
32    dxfId = Integer(allow_none=True)
33
34    def __init__(self,
35                 type=None,
36                 size=None,
37                 dxfId=None,
38                ):
39        self.type = type
40        self.size = size
41        self.dxfId = dxfId
42
43
44class TableStyle(Serialisable):
45
46    tagname = "tableStyle"
47
48    name = String()
49    pivot = Bool(allow_none=True)
50    table = Bool(allow_none=True)
51    count = Integer(allow_none=True)
52    tableStyleElement = Sequence(expected_type=TableStyleElement, allow_none=True)
53
54    __elements__ = ('tableStyleElement',)
55
56    def __init__(self,
57                 name=None,
58                 pivot=None,
59                 table=None,
60                 count=None,
61                 tableStyleElement=(),
62                ):
63        self.name = name
64        self.pivot = pivot
65        self.table = table
66        self.count = count
67        self.tableStyleElement = tableStyleElement
68
69
70class TableStyleList(Serialisable):
71
72    tagname = "tableStyles"
73
74    defaultTableStyle = String(allow_none=True)
75    defaultPivotStyle = String(allow_none=True)
76    tableStyle = Sequence(expected_type=TableStyle, allow_none=True)
77
78    __elements__ = ('tableStyle',)
79    __attrs__ = ("count", "defaultTableStyle", "defaultPivotStyle")
80
81    def __init__(self,
82                 count=None,
83                 defaultTableStyle="TableStyleMedium9",
84                 defaultPivotStyle="PivotStyleLight16",
85                 tableStyle=(),
86                ):
87        self.defaultTableStyle = defaultTableStyle
88        self.defaultPivotStyle = defaultPivotStyle
89        self.tableStyle = tableStyle
90
91
92    @property
93    def count(self):
94        return len(self.tableStyle)