fonts.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (c) 2010-2024 openpyxl
  2. from openpyxl.descriptors import (
  3. Alias,
  4. Sequence,
  5. Integer
  6. )
  7. from openpyxl.descriptors.serialisable import Serialisable
  8. from openpyxl.descriptors.nested import (
  9. NestedValue,
  10. NestedBool,
  11. NestedNoneSet,
  12. NestedMinMax,
  13. NestedString,
  14. NestedInteger,
  15. NestedFloat,
  16. )
  17. from .colors import ColorDescriptor, Color, BLACK
  18. from openpyxl.compat import safe_string
  19. from openpyxl.xml.functions import Element, SubElement
  20. from openpyxl.xml.constants import SHEET_MAIN_NS
  21. def _no_value(tagname, value, namespace=None):
  22. if value:
  23. return Element(tagname, val=safe_string(value))
  24. class Font(Serialisable):
  25. """Font options used in styles."""
  26. UNDERLINE_DOUBLE = 'double'
  27. UNDERLINE_DOUBLE_ACCOUNTING = 'doubleAccounting'
  28. UNDERLINE_SINGLE = 'single'
  29. UNDERLINE_SINGLE_ACCOUNTING = 'singleAccounting'
  30. name = NestedString(allow_none=True)
  31. charset = NestedInteger(allow_none=True)
  32. family = NestedMinMax(min=0, max=14, allow_none=True)
  33. sz = NestedFloat(allow_none=True)
  34. size = Alias("sz")
  35. b = NestedBool(to_tree=_no_value)
  36. bold = Alias("b")
  37. i = NestedBool(to_tree=_no_value)
  38. italic = Alias("i")
  39. strike = NestedBool(allow_none=True)
  40. strikethrough = Alias("strike")
  41. outline = NestedBool(allow_none=True)
  42. shadow = NestedBool(allow_none=True)
  43. condense = NestedBool(allow_none=True)
  44. extend = NestedBool(allow_none=True)
  45. u = NestedNoneSet(values=('single', 'double', 'singleAccounting',
  46. 'doubleAccounting'))
  47. underline = Alias("u")
  48. vertAlign = NestedNoneSet(values=('superscript', 'subscript', 'baseline'))
  49. color = ColorDescriptor(allow_none=True)
  50. scheme = NestedNoneSet(values=("major", "minor"))
  51. tagname = "font"
  52. __elements__ = ('name', 'charset', 'family', 'b', 'i', 'strike', 'outline',
  53. 'shadow', 'condense', 'color', 'extend', 'sz', 'u', 'vertAlign',
  54. 'scheme')
  55. def __init__(self, name=None, sz=None, b=None, i=None, charset=None,
  56. u=None, strike=None, color=None, scheme=None, family=None, size=None,
  57. bold=None, italic=None, strikethrough=None, underline=None,
  58. vertAlign=None, outline=None, shadow=None, condense=None,
  59. extend=None):
  60. self.name = name
  61. self.family = family
  62. if size is not None:
  63. sz = size
  64. self.sz = sz
  65. if bold is not None:
  66. b = bold
  67. self.b = b
  68. if italic is not None:
  69. i = italic
  70. self.i = i
  71. if underline is not None:
  72. u = underline
  73. self.u = u
  74. if strikethrough is not None:
  75. strike = strikethrough
  76. self.strike = strike
  77. self.color = color
  78. self.vertAlign = vertAlign
  79. self.charset = charset
  80. self.outline = outline
  81. self.shadow = shadow
  82. self.condense = condense
  83. self.extend = extend
  84. self.scheme = scheme
  85. @classmethod
  86. def from_tree(cls, node):
  87. """
  88. Set default value for underline if child element is present
  89. """
  90. underline = node.find("{%s}u" % SHEET_MAIN_NS)
  91. if underline is not None and underline.get('val') is None:
  92. underline.set("val", "single")
  93. return super().from_tree(node)
  94. DEFAULT_FONT = Font(name="Calibri", sz=11, family=2, b=False, i=False,
  95. color=Color(theme=1), scheme="minor")