text.py 1.8 KB

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