comments.py 1.5 KB

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