alignment.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (c) 2010-2024 openpyxl
  2. from openpyxl.compat import safe_string
  3. from openpyxl.descriptors import Bool, MinMax, Min, Alias, NoneSet
  4. from openpyxl.descriptors.serialisable import Serialisable
  5. horizontal_alignments = (
  6. "general", "left", "center", "right", "fill", "justify", "centerContinuous",
  7. "distributed", )
  8. vertical_aligments = (
  9. "top", "center", "bottom", "justify", "distributed",
  10. )
  11. class Alignment(Serialisable):
  12. """Alignment options for use in styles."""
  13. tagname = "alignment"
  14. horizontal = NoneSet(values=horizontal_alignments)
  15. vertical = NoneSet(values=vertical_aligments)
  16. textRotation = NoneSet(values=range(181))
  17. textRotation.values.add(255)
  18. text_rotation = Alias('textRotation')
  19. wrapText = Bool(allow_none=True)
  20. wrap_text = Alias('wrapText')
  21. shrinkToFit = Bool(allow_none=True)
  22. shrink_to_fit = Alias('shrinkToFit')
  23. indent = MinMax(min=0, max=255)
  24. relativeIndent = MinMax(min=-255, max=255)
  25. justifyLastLine = Bool(allow_none=True)
  26. readingOrder = Min(min=0)
  27. def __init__(self, horizontal=None, vertical=None,
  28. textRotation=0, wrapText=None, shrinkToFit=None, indent=0, relativeIndent=0,
  29. justifyLastLine=None, readingOrder=0, text_rotation=None,
  30. wrap_text=None, shrink_to_fit=None, mergeCell=None):
  31. self.horizontal = horizontal
  32. self.vertical = vertical
  33. self.indent = indent
  34. self.relativeIndent = relativeIndent
  35. self.justifyLastLine = justifyLastLine
  36. self.readingOrder = readingOrder
  37. if text_rotation is not None:
  38. textRotation = text_rotation
  39. if textRotation is not None:
  40. self.textRotation = int(textRotation)
  41. if wrap_text is not None:
  42. wrapText = wrap_text
  43. self.wrapText = wrapText
  44. if shrink_to_fit is not None:
  45. shrinkToFit = shrink_to_fit
  46. self.shrinkToFit = shrinkToFit
  47. # mergeCell is vestigial
  48. def __iter__(self):
  49. for attr in self.__attrs__:
  50. value = getattr(self, attr)
  51. if value is not None and value != 0:
  52. yield attr, safe_string(value)