alignment.py 2.5 KB

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