1# Copyright (c) 2010-2024 openpyxl
2
3from openpyxl.descriptors.serialisable import Serialisable
4from openpyxl.descriptors import (
5 Typed,
6 Alias,
7)
8
9from openpyxl.descriptors.excel import ExtensionList
10from openpyxl.descriptors.nested import NestedBool
11
12from .text import Text, RichText
13from .layout import Layout
14from .shapes import GraphicalProperties
15
16from openpyxl.drawing.text import (
17 Paragraph,
18 RegularTextRun,
19 LineBreak,
20 ParagraphProperties,
21 CharacterProperties,
22)
23
24
25class Title(Serialisable):
26 tagname = "title"
27
28 tx = Typed(expected_type=Text, allow_none=True)
29 text = Alias('tx')
30 layout = Typed(expected_type=Layout, allow_none=True)
31 overlay = NestedBool(allow_none=True)
32 spPr = Typed(expected_type=GraphicalProperties, allow_none=True)
33 graphicalProperties = Alias('spPr')
34 txPr = Typed(expected_type=RichText, allow_none=True)
35 body = Alias('txPr')
36 extLst = Typed(expected_type=ExtensionList, allow_none=True)
37
38 __elements__ = ('tx', 'layout', 'overlay', 'spPr', 'txPr')
39
40 def __init__(self,
41 tx=None,
42 layout=None,
43 overlay=None,
44 spPr=None,
45 txPr=None,
46 extLst=None,
47 ):
48 if tx is None:
49 tx = Text()
50 self.tx = tx
51 self.layout = layout
52 self.overlay = overlay
53 self.spPr = spPr
54 self.txPr = txPr
55
56
57
58def title_maker(text):
59 title = Title()
60 paraprops = ParagraphProperties()
61 paraprops.defRPr = CharacterProperties()
62 paras = [Paragraph(r=[RegularTextRun(t=s)], pPr=paraprops) for s in text.split("\n")]
63
64 title.tx.rich.paragraphs = paras
65 return title
66
67
68class TitleDescriptor(Typed):
69
70 expected_type = Title
71 allow_none = True
72
73 def __set__(self, instance, value):
74 if isinstance(value, str):
75 value = title_maker(value)
76 super().__set__(instance, value)