Can’t set `None` to cell.hyperlink

Issue #697 resolved
Laurent LAPORTE created an issue

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:

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
        if not isinstance(val, Hyperlink):
            val = Hyperlink(ref="", target=val)
        val.ref = self.coordinate
        self._hyperlink = val
        if self._value is None:
            self.value = or val.location

Comments (6)

  1. Log in to comment