Source code

Download source archive ↓

src/openpyxl/cell/text.py

1# Copyright (c) 2010-2024 openpyxl
2
3"""
4Richtext definition
5"""
6
7from openpyxl.descriptors.serialisable import Serialisable
8from openpyxl.descriptors import (
9    Alias,
10    Typed,
11    Integer,
12    Set,
13    NoneSet,
14    Bool,
15    String,
16    Sequence,
17)
18from openpyxl.descriptors.nested import (
19    NestedBool,
20    NestedInteger,
21    NestedString,
22    NestedText,
23)
24from openpyxl.styles.fonts import Font
25
26
27class PhoneticProperties(Serialisable):
28
29    tagname = "phoneticPr"
30
31    fontId = Integer()
32    type = NoneSet(values=(['halfwidthKatakana', 'fullwidthKatakana',
33                            'Hiragana', 'noConversion']))
34    alignment = NoneSet(values=(['noControl', 'left', 'center', 'distributed']))
35
36    def __init__(self,
37                 fontId=None,
38                 type=None,
39                 alignment=None,
40                ):
41        self.fontId = fontId
42        self.type = type
43        self.alignment = alignment
44
45
46class PhoneticText(Serialisable):
47
48    tagname = "rPh"
49
50    sb = Integer()
51    eb = Integer()
52    t = NestedText(expected_type=str)
53    text = Alias('t')
54
55    def __init__(self,
56                 sb=None,
57                 eb=None,
58                 t=None,
59                ):
60        self.sb = sb
61        self.eb = eb
62        self.t = t
63
64
65class InlineFont(Font):
66
67    """
68    Font for inline text because, yes what you need are different objects with the same elements but different constraints.
69    """
70
71    tagname = "RPrElt"
72
73    rFont = NestedString(allow_none=True)
74    charset = Font.charset
75    family = Font.family
76    b =Font.b
77    i = Font.i
78    strike = Font.strike
79    outline = Font.outline
80    shadow = Font.shadow
81    condense = Font.condense
82    extend = Font.extend
83    color = Font.color
84    sz = Font.sz
85    u = Font.u
86    vertAlign = Font.vertAlign
87    scheme = Font.scheme
88
89    __elements__ = ('rFont', 'charset', 'family', 'b', 'i', 'strike',
90                    'outline', 'shadow', 'condense', 'extend', 'color', 'sz', 'u',
91                    'vertAlign', 'scheme')
92
93    def __init__(self,
94                 rFont=None,
95                 charset=None,
96                 family=None,
97                 b=None,
98                 i=None,
99                 strike=None,
100                 outline=None,
101                 shadow=None,
102                 condense=None,
103                 extend=None,
104                 color=None,
105                 sz=None,
106                 u=None,
107                 vertAlign=None,
108                 scheme=None,
109                ):
110        self.rFont = rFont
111        self.charset = charset
112        self.family = family
113        self.b = b
114        self.i = i
115        self.strike = strike
116        self.outline = outline
117        self.shadow = shadow
118        self.condense = condense
119        self.extend = extend
120        self.color = color
121        self.sz = sz
122        self.u = u
123        self.vertAlign = vertAlign
124        self.scheme = scheme
125
126
127class RichText(Serialisable):
128
129    tagname = "RElt"
130
131    rPr = Typed(expected_type=InlineFont, allow_none=True)
132    font = Alias("rPr")
133    t = NestedText(expected_type=str, allow_none=True)
134    text = Alias("t")
135
136    __elements__ = ('rPr', 't')
137
138    def __init__(self,
139                 rPr=None,
140                 t=None,
141                ):
142        self.rPr = rPr
143        self.t = t
144
145
146class Text(Serialisable):
147
148    tagname = "text"
149
150    t = NestedText(allow_none=True, expected_type=str)
151    plain = Alias("t")
152    r = Sequence(expected_type=RichText, allow_none=True)
153    formatted = Alias("r")
154    rPh = Sequence(expected_type=PhoneticText, allow_none=True)
155    phonetic = Alias("rPh")
156    phoneticPr = Typed(expected_type=PhoneticProperties, allow_none=True)
157    PhoneticProperties = Alias("phoneticPr")
158
159    __elements__ = ('t', 'r', 'rPh', 'phoneticPr')
160
161    def __init__(self,
162                 t=None,
163                 r=(),
164                 rPh=(),
165                 phoneticPr=None,
166                ):
167        self.t = t
168        self.r = r
169        self.rPh = rPh
170        self.phoneticPr = phoneticPr
171
172
173    @property
174    def content(self):
175        """
176        Text stripped of all formatting
177        """
178        snippets = []
179        if self.plain is not None:
180            snippets.append(self.plain)
181        for block in self.formatted:
182            if block.t is not None:
183                snippets.append(block.t)
184        return u"".join(snippets)