title.py 1.9 KB

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