text.py 1.9 KB

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