1# Copyright (c) 2010-2024 openpyxl
2from openpyxl.descriptors.serialisable import Serialisable
3from openpyxl.descriptors import (
4 Typed,
5 Alias,
6 Sequence,
7)
8
9
10from openpyxl.drawing.text import (
11 RichTextProperties,
12 ListStyle,
13 Paragraph,
14)
15
16from .data_source import StrRef
17
18
19class RichText(Serialisable):
20
21 """
22 From the specification: 21.2.2.216
23
24 This element specifies text formatting. The lstStyle element is not supported.
25 """
26
27 tagname = "rich"
28
29 bodyPr = Typed(expected_type=RichTextProperties)
30 properties = Alias("bodyPr")
31 lstStyle = Typed(expected_type=ListStyle, allow_none=True)
32 p = Sequence(expected_type=Paragraph)
33 paragraphs = Alias('p')
34
35 __elements__ = ("bodyPr", "lstStyle", "p")
36
37 def __init__(self,
38 bodyPr=None,
39 lstStyle=None,
40 p=None,
41 ):
42 if bodyPr is None:
43 bodyPr = RichTextProperties()
44 self.bodyPr = bodyPr
45 self.lstStyle = lstStyle
46 if p is None:
47 p = [Paragraph()]
48 self.p = p
49
50
51class Text(Serialisable):
52
53 """
54 The value can be either a cell reference or a text element
55 If both are present then the reference will be used.
56 """
57
58 tagname = "tx"
59
60 strRef = Typed(expected_type=StrRef, allow_none=True)
61 rich = Typed(expected_type=RichText, allow_none=True)
62
63 __elements__ = ("strRef", "rich")
64
65 def __init__(self,
66 strRef=None,
67 rich=None
68 ):
69 self.strRef = strRef
70 if rich is None:
71 rich = RichText()
72 self.rich = rich
73
74
75 def to_tree(self, tagname=None, idx=None, namespace=None):
76 if self.strRef and self.rich:
77 self.rich = None # can only have one
78 return super().to_tree(tagname, idx, namespace)