comments.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (c) 2010-2024 openpyxl
  2. class Comment:
  3. _parent = None
  4. def __init__(self, text, author, height=79, width=144):
  5. self.content = text
  6. self.author = author
  7. self.height = height
  8. self.width = width
  9. @property
  10. def parent(self):
  11. return self._parent
  12. def __eq__(self, other):
  13. return (
  14. self.content == other.content
  15. and self.author == other.author
  16. )
  17. def __repr__(self):
  18. return "Comment: {0} by {1}".format(self.content, self.author)
  19. def __copy__(self):
  20. """Create a detached copy of this comment."""
  21. clone = self.__class__(self.content, self.author, self.height, self.width)
  22. return clone
  23. def bind(self, cell):
  24. """
  25. Bind comment to a particular cell
  26. """
  27. if cell is not None and self._parent is not None and self._parent != cell:
  28. fmt = "Comment already assigned to {0} in worksheet {1}. Cannot assign a comment to more than one cell"
  29. raise AttributeError(fmt.format(cell.coordinate, cell.parent.title))
  30. self._parent = cell
  31. def unbind(self):
  32. """
  33. Unbind a comment from a cell
  34. """
  35. self._parent = None
  36. @property
  37. def text(self):
  38. """
  39. Any comment text stripped of all formatting.
  40. """
  41. return self.content
  42. @text.setter
  43. def text(self, value):
  44. self.content = value