'MergedCell' object attribute 'comment' is read-only

Issue #1291 resolved
Piotr Dąbrowski created an issue

I have a problem with opening some file. Trying to open it with read_only=True finishes without any problems, but I need to have an info whether a cell/row/column is hidden and currently don't see any other way than to load the file with read_only=False. I've stripped any unnecessary for this issue content from the file and attached as a test_file.xlsx. The file contain only one merged cell and does not open properly.

In [1]: from openpyxl import load_workbook                                                                          

In [2]: load_workbook('/home/piotr/Desktop/test_file.xlsx')                                                         
AttributeError                            Traceback (most recent call last)
<ipython-input-2-48edaf6f6504> in <module>
----> 1 load_workbook('/home/piotr/Desktop/test_file.xlsx')

~/miniconda3/envs/openpyxl/lib/python3.7/site-packages/openpyxl/reader/excel.py in load_workbook(filename, read_only, keep_vba, data_only, keep_links)
    310     reader = ExcelReader(filename, read_only, keep_vba,
    311                         data_only, keep_links)
--> 312     reader.read()
    313     return reader.wb

~/miniconda3/envs/openpyxl/lib/python3.7/site-packages/openpyxl/reader/excel.py in read(self)
    272         self.read_theme()
    273         apply_stylesheet(self.archive, self.wb)
--> 274         self.read_worksheets()
    275         self.parser.assign_names()
    276         if not self.read_only:

~/miniconda3/envs/openpyxl/lib/python3.7/site-packages/openpyxl/reader/excel.py in read_worksheets(self)
    233                 comment_sheet = CommentSheet.from_tree(fromstring(src))
    234                 for ref, comment in comment_sheet.comments:
--> 235                     ws[ref].comment = comment
    237             # preserve link to VML file if VBA

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

In [4]: openpyxl.__version__                                                                                        
Out[4]: '2.6.2'

Comments (5)

  1. CharlieC

    Thanks for the report and the file. This is precisely the kind of situation which illustrates the problems with how Excel handles merged cells. B1, and all associated data, should be all rights have been removed from the worksheet but this hasn’t happened to the comment.

    This raises the question: what should happen with the comment when openpyxl reads the file? Should the exception be kept so that the user has the chance to decide? Should the comment be removed but a warning be given? Should the comment be removed silently? I’d tend towards the second option, because users should always know when data is being removed.

    Regarding the information available in read-only mode. It would be fairly easy to add support for ColumnDimensions as this is at the top of the XML. RowDimensions are more tricky as these are stored per row. This could be available on a row-by-row basis but would mean a different API to standard mode.

  2. Piotr Dąbrowski reporter

    Thank you for the reply! Unfortunately I’m in the need for the information of hidden rows.

    As for the solution for the MergedCell problem - I'm leaning towards returning all the data the file have, so letting user to decide. Warning solution is also quite right in my opinion.

  3. CharlieC

    MergedCell don’t support comments for the reasons listed above so a warning is the best that’s possible.

    It would be probably be possible to fill the RowDimensions dictionary as we go, as we already do with shared formulae so that you could loop over the rows in question and then check for entries.

  4. Log in to comment