differential.py 2.2 KB

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