title.py 2.0 KB

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