Skip to content

AttributeError: 'MergedCell' object attribute 'hyperlink' is read-only

Bitbucket Importer requested to merge bitbucket/merged-pr-327 into branch/2.6

Created originally on Bitbucket by Dmitry Ferens (Dmitriy Ferens)

Was already merged in Bitbucket before import, marked as merged by the import user

Hi,

I found that openpyxl fails to load spreadsheets, which contains links which made on merged cells.

Example: bugfix-merged-link.xlsx

Traceback:

  File "my-project.py", line 16, in get_spreadsheet
    wb = openpyxl.load_workbook(file_path)
  File "site-packages/openpyxl/reader/excel.py", line 312, in load_workbook
    reader.read()
  File "site-packages/openpyxl/reader/excel.py", line 274, in read
    self.read_worksheets()
  File "site-packages/openpyxl/reader/excel.py", line 228, in read_worksheets
    ws_parser.bind_all()
  File "site-packages/openpyxl/worksheet/_reader.py", line 403, in bind_all
    self.bind_hyperlinks()
  File "site-packages/openpyxl/worksheet/_reader.py", line 369, in bind_hyperlinks
    cell.hyperlink = link
AttributeError: 'MergedCell' object attribute 'hyperlink' is read-only

It fails to set an attribute on a MergedCell instance, on second loop iteration:

cell       = <MergedCell 'Sheet'.B18>
link       = <openpyxl.worksheet.hyperlink.Hyperlink object>
Parameters:
ref='A18', location=None, tooltip=None, display='http://test.com', id=None
rel        = <openpyxl.packaging.relationship.Relationship object>
Parameters:
Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink', Target='../', TargetMode=None, Id='rId1'
row        = (<Cell 'Sheet'.A18>, <MergedCell 'Sheet'.B18>)

A proposed fix is to remove that loop completely. I checked coverage and I found that tests never check such case (row has always 1 cell):

I added a possible test improvement and a possible solution, could you please take a look?

Dmitry

Merge request reports

Loading