Got "Could not open xxx.xlsx because some content is unreadable" message when i open a new xlsx file

Issue #888 invalid
Chris Tian created an issue

I wrote a excel file by openpyxl, when i opened the file, i got the "Could not open xxx.xlsx because some content is unreadable....." message, look at the following picture. excel_double_click.jpeg

Then i clicked "Open and Repair" button to open the problematic excel file. i found the problem came from the table title, all the unicode(Chinese) string display normally, but it's existing problem in fact, look at the following picture. excel_title_unicode.jpeg

The openpyxl version which i used is 2.4.8 on Redhat Enterprise Linux 7.3 x86-64bit.

Comments (10)

  1. Chris Tian reporter
    • changed version to 2.4.x
    # -*- coding:utf-8 -*-
    import openpyxl
    from openpyxl import styles
    from openpyxl.worksheet import table
    
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    excel_file = openpyxl.Workbook()
    worksheet_actived = excel_file.active
    worksheet_actived.title = "demo"
    serverslist_table_start_row_num = 7
    serverslist_table_end_row_num = serverslist_table_start_row_num + len(wlserver_config_mbeans)
     serverslist_table_style = table.TableStyleInfo(name="TableStyleMedium9", showRowStripes=True)
    serverslist_table = table.Table(displayName=u'WLServer列表',
                            ref="A" + str(serverslist_table_start_row_num) + ":F" + str(serverslist_table_end_row_num),
                            tableStyleInfo=serverslist_table_style)
    servers_table_title = [u'序号', u'Server名称', u'Server监听地址', u'Server监听端口', u'Server启动状态', u'Server健康状态']
    for index, item in enumerate(servers_table_title):
           worksheet_actived[string.ascii_uppercase[index] + str(serverslist_table_start_row_num)] = item
           worksheet_actived[string.ascii_uppercase[index] + str(serverslist_table_start_row_num)].font = styles.Font(name='Microsoft YaHei', color=styles.colors.WHITE, bold=True)
    worksheet_actived.add_table(serverslist_table)
    excel_file.save('wls-daily-check.xlsx')
    
  2. CharlieC

    Did you ignore the warning that openpyxl gives you?

    The supplied is pretty impenetrable but with some slight changes I was able to create a table without any problems. It looks like the way you add headers is broken as these go from B6:G7. It's always best to let create cells wherever possible.

    import openpyxl
    from openpyxl import styles
    from openpyxl.worksheet import table
    
    wb = openpyxl.Workbook()
    ws = wb.active
    ws.title = "demo"
    table_start_row = 7
    wlserver_config_mbeans = 4 # based on original code
    table_end_row = table_start_row + wlserver_config_mbeans
    
    table_style = table.TableStyleInfo(name="TableStyleMedium9", showRowStripes=True)
    table = table.Table(displayName=u'WLServe',
                            ref="A{0}:F{1}".format(table_start_row, table_end_row),
                            tableStyleInfo=table_style)
    
    servers_table_title = [u'序号', u'Server名称', u'Server监听地址', u'Server监听端口', u'Server启动状态', u'Server健康状态']
    
    ws['A6'] = None
    ws.append(servers_table_title)
    
    header_font = styles.Font(name='Microsoft YaHei', color=styles.colors.WHITE, bold=True)
    for cell in ws[6]:
        cell.font = header_font
    
    ws.add_table(table)
    wb.save('bug899.xlsx')
    
  3. Chris Tian reporter

    I found the reason for incorrect unicode string was Jython, i run the same code under Python 2.7.5, it was working fine, when i run it under Jython 2.7.1, the unicode title string display normally, but it's existing problem in fact. unicode_under_python_jython

  4. Chris Tian reporter

    The issue is resolved by updating openpyxl version to 2.4.9 without code changing.

    The issue may be related with bug #842,and the bug is fixed in 2.4.9, when i updated to 2.4.9, my issue is resolved too.

    Thank you very much.

  5. Log in to comment