Source code

Download source archive ↓

src/openpyxl/styles/fonts.py

1# Copyright (c) 2010-2024 openpyxl
2
3
4from openpyxl.descriptors import (
5    Alias,
6    Sequence,
7    Integer
8)
9from openpyxl.descriptors.serialisable import Serialisable
10
11from openpyxl.descriptors.nested import (
12    NestedValue,
13    NestedBool,
14    NestedNoneSet,
15    NestedMinMax,
16    NestedString,
17    NestedInteger,
18    NestedFloat,
19)
20from .colors import ColorDescriptor, Color, BLACK
21
22from openpyxl.compat import safe_string
23from openpyxl.xml.functions import Element, SubElement
24from openpyxl.xml.constants import SHEET_MAIN_NS
25
26
27def _no_value(tagname, value, namespace=None):
28    if value:
29        return Element(tagname, val=safe_string(value))
30
31
32class Font(Serialisable):
33    """Font options used in styles."""
34
35    UNDERLINE_DOUBLE = 'double'
36    UNDERLINE_DOUBLE_ACCOUNTING = 'doubleAccounting'
37    UNDERLINE_SINGLE = 'single'
38    UNDERLINE_SINGLE_ACCOUNTING = 'singleAccounting'
39
40    name = NestedString(allow_none=True)
41    charset = NestedInteger(allow_none=True)
42    family = NestedMinMax(min=0, max=14, allow_none=True)
43    sz = NestedFloat(allow_none=True)
44    size = Alias("sz")
45    b = NestedBool(to_tree=_no_value)
46    bold = Alias("b")
47    i = NestedBool(to_tree=_no_value)
48    italic = Alias("i")
49    strike = NestedBool(allow_none=True)
50    strikethrough = Alias("strike")
51    outline = NestedBool(allow_none=True)
52    shadow = NestedBool(allow_none=True)
53    condense = NestedBool(allow_none=True)
54    extend = NestedBool(allow_none=True)
55    u = NestedNoneSet(values=('single', 'double', 'singleAccounting',
56                             'doubleAccounting'))
57    underline = Alias("u")
58    vertAlign = NestedNoneSet(values=('superscript', 'subscript', 'baseline'))
59    color = ColorDescriptor(allow_none=True)
60    scheme = NestedNoneSet(values=("major", "minor"))
61
62    tagname = "font"
63
64    __elements__ = ('name', 'charset', 'family', 'b', 'i', 'strike', 'outline',
65                  'shadow', 'condense', 'color', 'extend', 'sz', 'u', 'vertAlign',
66                  'scheme')
67
68
69    def __init__(self, name=None, sz=None, b=None, i=None, charset=None,
70                 u=None, strike=None, color=None, scheme=None, family=None, size=None,
71                 bold=None, italic=None, strikethrough=None, underline=None,
72                 vertAlign=None, outline=None, shadow=None, condense=None,
73                 extend=None):
74        self.name = name
75        self.family = family
76        if size is not None:
77            sz = size
78        self.sz = sz
79        if bold is not None:
80            b = bold
81        self.b = b
82        if italic is not None:
83            i = italic
84        self.i = i
85        if underline is not None:
86            u = underline
87        self.u = u
88        if strikethrough is not None:
89            strike = strikethrough
90        self.strike = strike
91        self.color = color
92        self.vertAlign = vertAlign
93        self.charset = charset
94        self.outline = outline
95        self.shadow = shadow
96        self.condense = condense
97        self.extend = extend
98        self.scheme = scheme
99
100
101    @classmethod
102    def from_tree(cls, node):
103        """
104        Set default value for underline if child element is present
105        """
106        underline = node.find("{%s}u" % SHEET_MAIN_NS)
107        if underline is not None and underline.get('val') is None:
108            underline.set("val", "single")
109        return super().from_tree(node)
110
111
112DEFAULT_FONT = Font(name="Calibri", sz=11, family=2, b=False, i=False,
113                    color=Color(theme=1), scheme="minor")