differential.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from __future__ import absolute_import
  2. # Copyright (c) 2010-2019 openpyxl
  3. from openpyxl.descriptors import (
  4. Integer,
  5. String,
  6. Typed,
  7. Sequence,
  8. Alias,
  9. )
  10. from openpyxl.descriptors.serialisable import Serialisable
  11. from openpyxl.styles import (
  12. Font,
  13. Fill,
  14. GradientFill,
  15. PatternFill,
  16. Border,
  17. Alignment,
  18. Protection,
  19. )
  20. from .numbers import NumberFormat
  21. class DifferentialStyle(Serialisable):
  22. tagname = "dxf"
  23. __elements__ = ("font", "numFmt", "fill", "alignment", "border", "protection")
  24. font = Typed(expected_type=Font, allow_none=True)
  25. numFmt = Typed(expected_type=NumberFormat, allow_none=True)
  26. fill = Typed(expected_type=Fill, allow_none=True)
  27. alignment = Typed(expected_type=Alignment, allow_none=True)
  28. border = Typed(expected_type=Border, allow_none=True)
  29. protection = Typed(expected_type=Protection, allow_none=True)
  30. def __init__(self,
  31. font=None,
  32. numFmt=None,
  33. fill=None,
  34. alignment=None,
  35. border=None,
  36. protection=None,
  37. extLst=None,
  38. ):
  39. self.font = font
  40. self.numFmt = numFmt
  41. self.fill = fill
  42. self.alignment = alignment
  43. self.border = border
  44. self.protection = protection
  45. self.extLst = extLst
  46. class DifferentialStyleList(Serialisable):
  47. """
  48. Deduping container for differential styles.
  49. """
  50. tagname = "dxfs"
  51. dxf = Sequence(expected_type=DifferentialStyle)
  52. styles = Alias("dxf")
  53. def __init__(self, dxf=()):
  54. self.dxf = dxf
  55. def append(self, dxf):
  56. """
  57. Check to see whether style already exists and append it if does not.
  58. """
  59. if not isinstance(dxf, DifferentialStyle):
  60. raise TypeError('expected ' + str(DifferentialStyle))
  61. if dxf in self.styles:
  62. return
  63. self.styles.append(dxf)
  64. def add(self, dxf):
  65. """
  66. Add a differential style and return its index
  67. """
  68. self.append(dxf)
  69. return self.styles.index(dxf)
  70. def __bool__(self):
  71. return bool(self.styles)
  72. __nonzero__ = __bool__
  73. def __getitem__(self, idx):
  74. return self.styles[idx]