fonts.py 3.5 KB

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