Source code

Download source archive ↓

src/openpyxl/styles/alignment.py

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