The copy of a Comment should return a detached clone
Created originally on Bitbucket by Tantale (Laurent LAPORTE)
It is impossible to copy a comment from a source cell to a destination cell because the copy of the comment is still attached with the source.
The following unit test fails:
def test_copy():
wb = Workbook()
ws = Worksheet(wb)
source = Comment("text", "author")
ws.cell(row=1, column=1).comment = source
clone = copy(source)
ws.cell(row=2, column=1).comment = clone
assert clone._parent == ws.cell(row=2, column=1)
assert clone.text == "text"
assert clone.author == "author"
With the error: "AttributeError: Comment already assigned to A2 in worksheet Sheet1. Cannot assign a comment to more than one cell"
The solution is to implement the __copy__
operator for openpyxl.comments.comments.Comment
class:
def __copy__(self):
"""Create a detached copy of this comment."""
clone = self.__class__(self.content, self.author)
clone.width = self.width
clone.height = self.height
return clone