text.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. from __future__ import absolute_import
  2. # Copyright (c) 2010-2019 openpyxl
  3. """
  4. Richtext definition
  5. """
  6. from openpyxl.compat import unicode
  7. from openpyxl.descriptors.serialisable import Serialisable
  8. from openpyxl.descriptors import (
  9. Alias,
  10. Typed,
  11. Integer,
  12. Set,
  13. NoneSet,
  14. Bool,
  15. String,
  16. Sequence,
  17. )
  18. from openpyxl.descriptors.nested import (
  19. NestedBool,
  20. NestedInteger,
  21. NestedString,
  22. NestedText,
  23. )
  24. from openpyxl.styles.fonts import Font
  25. class PhoneticProperties(Serialisable):
  26. tagname = "phoneticPr"
  27. fontId = Integer()
  28. type = NoneSet(values=(['halfwidthKatakana', 'fullwidthKatakana',
  29. 'Hiragana', 'noConversion']))
  30. alignment = NoneSet(values=(['noControl', 'left', 'center', 'distributed']))
  31. def __init__(self,
  32. fontId=None,
  33. type=None,
  34. alignment=None,
  35. ):
  36. self.fontId = fontId
  37. self.type = type
  38. self.alignment = alignment
  39. class PhoneticText(Serialisable):
  40. tagname = "rPh"
  41. sb = Integer()
  42. eb = Integer()
  43. t = NestedText(expected_type=unicode)
  44. text = Alias('t')
  45. def __init__(self,
  46. sb=None,
  47. eb=None,
  48. t=None,
  49. ):
  50. self.sb = sb
  51. self.eb = eb
  52. self.t = t
  53. class InlineFont(Font):
  54. """
  55. Font for inline text because, yes what you need are different objects with the same elements but different constraints.
  56. """
  57. tagname = "RPrElt"
  58. rFont = NestedString(allow_none=True)
  59. charset = Font.charset
  60. family = Font.family
  61. b =Font.b
  62. i = Font.i
  63. strike = Font.strike
  64. outline = Font.outline
  65. shadow = Font.shadow
  66. condense = Font.condense
  67. extend = Font.extend
  68. color = Font.color
  69. sz = Font.sz
  70. u = Font.u
  71. vertAlign = Font.vertAlign
  72. scheme = Font.scheme
  73. __elements__ = ('rFont', 'charset', 'family', 'b', 'i', 'strike',
  74. 'outline', 'shadow', 'condense', 'extend', 'color', 'sz', 'u',
  75. 'vertAlign', 'scheme')
  76. def __init__(self,
  77. rFont=None,
  78. charset=None,
  79. family=None,
  80. b=None,
  81. i=None,
  82. strike=None,
  83. outline=None,
  84. shadow=None,
  85. condense=None,
  86. extend=None,
  87. color=None,
  88. sz=None,
  89. u=None,
  90. vertAlign=None,
  91. scheme=None,
  92. ):
  93. self.rFont = rFont
  94. self.charset = charset
  95. self.family = family
  96. self.b = b
  97. self.i = i
  98. self.strike = strike
  99. self.outline = outline
  100. self.shadow = shadow
  101. self.condense = condense
  102. self.extend = extend
  103. self.color = color
  104. self.sz = sz
  105. self.u = u
  106. self.vertAlign = vertAlign
  107. self.scheme = scheme
  108. class RichText(Serialisable):
  109. tagname = "RElt"
  110. rPr = Typed(expected_type=InlineFont, allow_none=True)
  111. font = Alias("rPr")
  112. t = NestedText(expected_type=unicode, allow_none=True)
  113. text = Alias("t")
  114. __elements__ = ('rPr', 't')
  115. def __init__(self,
  116. rPr=None,
  117. t=None,
  118. ):
  119. self.rPr = rPr
  120. self.t = t
  121. class Text(Serialisable):
  122. tagname = "text"
  123. t = NestedText(allow_none=True, expected_type=unicode)
  124. plain = Alias("t")
  125. r = Sequence(expected_type=RichText, allow_none=True)
  126. formatted = Alias("r")
  127. rPh = Sequence(expected_type=PhoneticText, allow_none=True)
  128. phonetic = Alias("rPh")
  129. phoneticPr = Typed(expected_type=PhoneticProperties, allow_none=True)
  130. PhoneticProperties = Alias("phoneticPr")
  131. __elements__ = ('t', 'r', 'rPh', 'phoneticPr')
  132. def __init__(self,
  133. t=None,
  134. r=(),
  135. rPh=(),
  136. phoneticPr=None,
  137. ):
  138. self.t = t
  139. self.r = r
  140. self.rPh = rPh
  141. self.phoneticPr = phoneticPr
  142. @property
  143. def content(self):
  144. """
  145. Text stripped of all formatting
  146. """
  147. snippets = []
  148. if self.plain is not None:
  149. snippets.append(self.plain)
  150. for block in self.formatted:
  151. if block.t is not None:
  152. snippets.append(block.t)
  153. return u"".join(snippets)