Can’t set `None` to cell.hyperlink
Created originally on Bitbucket by Tantale (Laurent LAPORTE)
When we set None
to a cell.hyperlink, it creates an Hyperlink with a None
target instead of setting None
.
The following unit test fails:
def test_hyperlink_none(dummy_cell):
cell = dummy_cell
assert cell.hyperlink is None # OK
cell.hyperlink = None
assert cell.hyperlink is None # FAIL
The workaround:
cell._hyperlink = None
The fix in openpyxl.cell.cell.Cell.hyperlink
setter:
@hyperlink.setter
def hyperlink(self, val):
"""Set value and display for hyperlinks in a cell.
Automatically sets the `value` of the cell with link text,
but you can modify it afterwards by setting the `value`
property, and the hyperlink will remain.
Hyperlink is removed if set to ``None``."""
if val is None:
self._hyperlink = None
else:
if not isinstance(val, Hyperlink):
val = Hyperlink(ref="", target=val)
val.ref = self.coordinate
self._hyperlink = val
if self._value is None:
self.value = val.target or val.location