Source code

Download source archive ↓

src/openpyxl/comments/comments.py

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