read_only.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. from __future__ import absolute_import
  2. # Copyright (c) 2010-2019 openpyxl
  3. from openpyxl.cell import Cell
  4. from openpyxl.utils import get_column_letter
  5. from openpyxl.utils.datetime import from_excel
  6. from openpyxl.styles import is_date_format
  7. from openpyxl.styles.numbers import BUILTIN_FORMATS, BUILTIN_FORMATS_MAX_SIZE
  8. class ReadOnlyCell(object):
  9. __slots__ = ('parent', 'row', 'column', '_value', 'data_type', '_style_id')
  10. def __init__(self, sheet, row, column, value, data_type='n', style_id=0):
  11. self.parent = sheet
  12. self._value = None
  13. self.row = row
  14. self.column = column
  15. self.data_type = data_type
  16. self.value = value
  17. self._style_id = style_id
  18. def __eq__(self, other):
  19. for a in self.__slots__:
  20. if getattr(self, a) != getattr(other, a):
  21. return
  22. return True
  23. def __ne__(self, other):
  24. return not self.__eq__(other)
  25. def __repr__(self):
  26. return "<ReadOnlyCell {0!r}.{1}>".format(self.parent.title, self.coordinate)
  27. @property
  28. def coordinate(self):
  29. column = get_column_letter(self.column)
  30. return "{1}{0}".format(self.row, column)
  31. @property
  32. def coordinate(self):
  33. return Cell.coordinate.__get__(self)
  34. @property
  35. def column_letter(self):
  36. return Cell.column_letter.__get__(self)
  37. @property
  38. def style_array(self):
  39. return self.parent.parent._cell_styles[self._style_id]
  40. @property
  41. def number_format(self):
  42. _id = self.style_array.numFmtId
  43. if _id < BUILTIN_FORMATS_MAX_SIZE:
  44. return BUILTIN_FORMATS.get(_id, "General")
  45. else:
  46. return self.parent.parent._number_formats[
  47. _id - BUILTIN_FORMATS_MAX_SIZE]
  48. @property
  49. def font(self):
  50. _id = self.style_array.fontId
  51. return self.parent.parent._fonts[_id]
  52. @property
  53. def fill(self):
  54. _id = self.style_array.fillId
  55. return self.parent.parent._fills[_id]
  56. @property
  57. def border(self):
  58. _id = self.style_array.borderId
  59. return self.parent.parent._borders[_id]
  60. @property
  61. def alignment(self):
  62. _id = self.style_array.alignmentId
  63. return self.parent.parent._alignments[_id]
  64. @property
  65. def protection(self):
  66. _id = self.style_array.protectionId
  67. return self.parent.parent._protections[_id]
  68. @property
  69. def is_date(self):
  70. return Cell.is_date.__get__(self)
  71. @property
  72. def internal_value(self):
  73. return self._value
  74. @property
  75. def value(self):
  76. return self._value
  77. @value.setter
  78. def value(self, value):
  79. if self._value is not None:
  80. raise AttributeError("Cell is read only")
  81. self._value = value
  82. class EmptyCell(object):
  83. __slots__ = ()
  84. value = None
  85. is_date = False
  86. font = None
  87. border = None
  88. fill = None
  89. number_format = None
  90. alignment = None
  91. data_type = 'n'
  92. def __repr__(self):
  93. return "<EmptyCell>"
  94. EMPTY_CELL = EmptyCell()