pagebreak.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (c) 2010-2019 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. __nonzero__ = __bool__
  43. def __len__(self):
  44. return len(self.brk)
  45. @property
  46. def count(self):
  47. return len(self)
  48. @property
  49. def manualBreakCount(self):
  50. return len(self)
  51. def append(self, brk=None):
  52. """
  53. Add a page break
  54. """
  55. vals = list(self.brk)
  56. if not isinstance(brk, Break):
  57. brk = Break(id=self.count+1)
  58. vals.append(brk)
  59. self.brk = vals
  60. PageBreak = RowBreak
  61. class ColBreak(RowBreak):
  62. tagname = "colBreaks"
  63. count = RowBreak.count
  64. manualBreakCount = RowBreak.manualBreakCount
  65. brk = RowBreak.brk
  66. __attrs__ = RowBreak.__attrs__