pagebreak.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (c) 2010-2024 openpyxl
  2. from openpyxl.descriptors.serialisable import Serialisable
  3. from openpyxl.descriptors import (
  4. Integer,
  5. Bool,
  6. Sequence,
  7. )
  8. class Break(Serialisable):
  9. tagname = "brk"
  10. id = Integer(allow_none=True)
  11. min = Integer(allow_none=True)
  12. max = Integer(allow_none=True)
  13. man = Bool(allow_none=True)
  14. pt = Bool(allow_none=True)
  15. def __init__(self,
  16. id=0,
  17. min=0,
  18. max=16383,
  19. man=True,
  20. pt=None,
  21. ):
  22. self.id = id
  23. self.min = min
  24. self.max = max
  25. self.man = man
  26. self.pt = pt
  27. class RowBreak(Serialisable):
  28. tagname = "rowBreaks"
  29. count = Integer(allow_none=True)
  30. manualBreakCount = Integer(allow_none=True)
  31. brk = Sequence(expected_type=Break, allow_none=True)
  32. __elements__ = ('brk',)
  33. __attrs__ = ("count", "manualBreakCount",)
  34. def __init__(self,
  35. count=None,
  36. manualBreakCount=None,
  37. brk=(),
  38. ):
  39. self.brk = brk
  40. def __bool__(self):
  41. return len(self.brk) > 0
  42. def __len__(self):
  43. return len(self.brk)
  44. @property
  45. def count(self):
  46. return len(self)
  47. @property
  48. def manualBreakCount(self):
  49. return len(self)
  50. def append(self, brk=None):
  51. """
  52. Add a page break
  53. """
  54. vals = list(self.brk)
  55. if not isinstance(brk, Break):
  56. brk = Break(id=self.count+1)
  57. vals.append(brk)
  58. self.brk = vals
  59. PageBreak = RowBreak
  60. class ColBreak(RowBreak):
  61. tagname = "colBreaks"
  62. count = RowBreak.count
  63. manualBreakCount = RowBreak.manualBreakCount
  64. brk = RowBreak.brk
  65. __attrs__ = RowBreak.__attrs__