read_only.py 3.0 KB

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