Comments are broken in write_only mode

Issue #618 resolved
Mark Cho
created an issue

Comments are broken on Workbook(write_only=True)

To reproduce:

wb = Workbook(write_only=True)
ws = wb.create_sheet()
cell = WriteOnlyCell(ws, 'Test 1')
cell.comment = Comment(text='Hello World', author='openpyxl')
headers = [cell]
ws.append(headers)
wb.save('test.xlsx')

This will save text.xlsx file but when I tried to open the file in Microsoft Excel, I get the following error (see attached image).

Log file from Excel:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>Repair Result to test 09449.xml</logFileName><summary>Errors were detected in file 'Macintosh HD:Users:...:test.xlsx'</summary><removedParts summary="Following is a list of removed parts:"><removedPart>Removed Part: Drawing shape.</removedPart></removedParts></recoveryLog>

Comments (4)

  1. MTV DNA

    Unfortunately, this didn't fix the problem entirely. Only the first comment is added to the worksheet correctly, but other comments still cause an error.

    Code used:

    from openpyxl import  Workbook
    from openpyxl.comments import Comment
    from openpyxl.writer.write_only import WriteOnlyCell
    
    wb = Workbook(write_only=True)
    ws = wb.create_sheet()
    
    for x in range(1,10):
        cell = WriteOnlyCell(ws)
        cell.value = 10*x
    
        cell.comment = Comment('Comment nr: {}'.format(x), 'Author {}'.format(x))
    
        cell2 = WriteOnlyCell(ws)
        cell2.value = 20*x
        cell2.comment = Comment('Comment column 2: {}'.format(x), 'Author {}'.format(x))
        ws.append([cell, cell2])
    
    wb.save("test.xlsx")
    

    Excel logfile:

    <?xml version="1.0" encoding="UTF-8" standalone="true"?>
    -<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <logFileName>error068920_01.xml</logFileName>
    <summary>Errors were detected in file 'C:\Users\s097116\Documents\Python Scripts\MDQT\v3\test.xlsx'</summary>
    -<removedRecords summary="Following is a list of removed records:">
    <removedRecord>Removed Records: Comments from /xl/comments1.xml part (Comments)</removedRecord>
    </removedRecords>
    </recoveryLog>
    
  2. Log in to comment