/tablib/packages/openpyxl/style.py
Python | 392 lines | 293 code | 57 blank | 42 comment | 7 complexity | f3216c8495818665db37bb778a7d4153 MD5 | raw file
- # file openpyxl/style.py
- # Copyright (c) 2010 openpyxl
- #
- # Permission is hereby granted, free of charge, to any person obtaining a copy
- # of this software and associated documentation files (the "Software"), to deal
- # in the Software without restriction, including without limitation the rights
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- # copies of the Software, and to permit persons to whom the Software is
- # furnished to do so, subject to the following conditions:
- #
- # The above copyright notice and this permission notice shall be included in
- # all copies or substantial portions of the Software.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- # THE SOFTWARE.
- #
- # @license: http://www.opensource.org/licenses/mit-license.php
- # @author: Eric Gazoni
- """Style and formatting option tracking."""
- # Python stdlib imports
- import re
- try:
- from hashlib import md5
- except ImportError:
- from md5 import md5
- class HashableObject(object):
- """Define how to hash property classes."""
- __fields__ = None
- __leaf__ = False
- def __repr__(self):
- return ':'.join([repr(getattr(self, x)) for x in self.__fields__])
- def __hash__(self):
- # return int(md5(repr(self)).hexdigest(), 16)
- return hash(repr(self))
- class Color(HashableObject):
- """Named colors for use in styles."""
- BLACK = 'FF000000'
- WHITE = 'FFFFFFFF'
- RED = 'FFFF0000'
- DARKRED = 'FF800000'
- BLUE = 'FF0000FF'
- DARKBLUE = 'FF000080'
- GREEN = 'FF00FF00'
- DARKGREEN = 'FF008000'
- YELLOW = 'FFFFFF00'
- DARKYELLOW = 'FF808000'
- __fields__ = ('index',)
- __slots__ = __fields__
- __leaf__ = True
- def __init__(self, index):
- super(Color, self).__init__()
- self.index = index
- class Font(HashableObject):
- """Font options used in styles."""
- UNDERLINE_NONE = 'none'
- UNDERLINE_DOUBLE = 'double'
- UNDERLINE_DOUBLE_ACCOUNTING = 'doubleAccounting'
- UNDERLINE_SINGLE = 'single'
- UNDERLINE_SINGLE_ACCOUNTING = 'singleAccounting'
- __fields__ = ('name',
- 'size',
- 'bold',
- 'italic',
- 'superscript',
- 'subscript',
- 'underline',
- 'strikethrough',
- 'color')
- __slots__ = __fields__
- def __init__(self):
- super(Font, self).__init__()
- self.name = 'Calibri'
- self.size = 11
- self.bold = False
- self.italic = False
- self.superscript = False
- self.subscript = False
- self.underline = self.UNDERLINE_NONE
- self.strikethrough = False
- self.color = Color(Color.BLACK)
- class Fill(HashableObject):
- """Area fill patterns for use in styles."""
- FILL_NONE = 'none'
- FILL_SOLID = 'solid'
- FILL_GRADIENT_LINEAR = 'linear'
- FILL_GRADIENT_PATH = 'path'
- FILL_PATTERN_DARKDOWN = 'darkDown'
- FILL_PATTERN_DARKGRAY = 'darkGray'
- FILL_PATTERN_DARKGRID = 'darkGrid'
- FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal'
- FILL_PATTERN_DARKTRELLIS = 'darkTrellis'
- FILL_PATTERN_DARKUP = 'darkUp'
- FILL_PATTERN_DARKVERTICAL = 'darkVertical'
- FILL_PATTERN_GRAY0625 = 'gray0625'
- FILL_PATTERN_GRAY125 = 'gray125'
- FILL_PATTERN_LIGHTDOWN = 'lightDown'
- FILL_PATTERN_LIGHTGRAY = 'lightGray'
- FILL_PATTERN_LIGHTGRID = 'lightGrid'
- FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal'
- FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis'
- FILL_PATTERN_LIGHTUP = 'lightUp'
- FILL_PATTERN_LIGHTVERTICAL = 'lightVertical'
- FILL_PATTERN_MEDIUMGRAY = 'mediumGray'
- __fields__ = ('fill_type',
- 'rotation',
- 'start_color',
- 'end_color')
- __slots__ = __fields__
- def __init__(self):
- super(Fill, self).__init__()
- self.fill_type = self.FILL_NONE
- self.rotation = 0
- self.start_color = Color(Color.WHITE)
- self.end_color = Color(Color.BLACK)
- class Border(HashableObject):
- """Border options for use in styles."""
- BORDER_NONE = 'none'
- BORDER_DASHDOT = 'dashDot'
- BORDER_DASHDOTDOT = 'dashDotDot'
- BORDER_DASHED = 'dashed'
- BORDER_DOTTED = 'dotted'
- BORDER_DOUBLE = 'double'
- BORDER_HAIR = 'hair'
- BORDER_MEDIUM = 'medium'
- BORDER_MEDIUMDASHDOT = 'mediumDashDot'
- BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot'
- BORDER_MEDIUMDASHED = 'mediumDashed'
- BORDER_SLANTDASHDOT = 'slantDashDot'
- BORDER_THICK = 'thick'
- BORDER_THIN = 'thin'
- __fields__ = ('border_style',
- 'color')
- __slots__ = __fields__
- def __init__(self):
- super(Border, self).__init__()
- self.border_style = self.BORDER_NONE
- self.color = Color(Color.BLACK)
- class Borders(HashableObject):
- """Border positioning for use in styles."""
- DIAGONAL_NONE = 0
- DIAGONAL_UP = 1
- DIAGONAL_DOWN = 2
- DIAGONAL_BOTH = 3
- __fields__ = ('left',
- 'right',
- 'top',
- 'bottom',
- 'diagonal',
- 'diagonal_direction',
- 'all_borders',
- 'outline',
- 'inside',
- 'vertical',
- 'horizontal')
- __slots__ = __fields__
- def __init__(self):
- super(Borders, self).__init__()
- self.left = Border()
- self.right = Border()
- self.top = Border()
- self.bottom = Border()
- self.diagonal = Border()
- self.diagonal_direction = self.DIAGONAL_NONE
- self.all_borders = Border()
- self.outline = Border()
- self.inside = Border()
- self.vertical = Border()
- self.horizontal = Border()
- class Alignment(HashableObject):
- """Alignment options for use in styles."""
- HORIZONTAL_GENERAL = 'general'
- HORIZONTAL_LEFT = 'left'
- HORIZONTAL_RIGHT = 'right'
- HORIZONTAL_CENTER = 'center'
- HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous'
- HORIZONTAL_JUSTIFY = 'justify'
- VERTICAL_BOTTOM = 'bottom'
- VERTICAL_TOP = 'top'
- VERTICAL_CENTER = 'center'
- VERTICAL_JUSTIFY = 'justify'
- __fields__ = ('horizontal',
- 'vertical',
- 'text_rotation',
- 'wrap_text',
- 'shrink_to_fit',
- 'indent')
- __slots__ = __fields__
- __leaf__ = True
- def __init__(self):
- super(Alignment, self).__init__()
- self.horizontal = self.HORIZONTAL_GENERAL
- self.vertical = self.VERTICAL_BOTTOM
- self.text_rotation = 0
- self.wrap_text = False
- self.shrink_to_fit = False
- self.indent = 0
- class NumberFormat(HashableObject):
- """Numer formatting for use in styles."""
- FORMAT_GENERAL = 'General'
- FORMAT_TEXT = '@'
- FORMAT_NUMBER = '0'
- FORMAT_NUMBER_00 = '0.00'
- FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00'
- FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-'
- FORMAT_PERCENTAGE = '0%'
- FORMAT_PERCENTAGE_00 = '0.00%'
- FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd'
-