jmcnamara / XlsxWriter

A Python module for creating Excel XLSX files.
https://xlsxwriter.readthedocs.io
BSD 2-Clause "Simplified" License
3.66k stars 631 forks source link

Issue with charts that fail to open in Microsoft 365 Excel #1044

Closed jsanchezf closed 8 months ago

jsanchezf commented 9 months ago

Hi,

I am using XlsxWriter to do create charts from some data but it appears that loading the result in Excel fails with the classical "Excel could not open file.xlsx because some content is unreadable. Do you want to open and repair this workbook."

I am using Python version 3.6.3 and XlsxWriter 3.2.0 and Excel version:

Microsoft® Excel® para Microsoft 365 MSO (versión 2311 compilación 16.0.17029.20140) de 32 bits

(It is a Spanish-language version)

Here is some code that demonstrates the problem:

import xlsxwriter
workbook = xlsxwriter.Workbook('chart_pie.xlsx')
worksheet = workbook.add_worksheet()

data = [
    ['Apple',  60],
    ['Cherry', 30],
    ['Pecan',  10],
]

worksheet.add_table('A1:B4', {'data': data,
                              'columns': [{'header': 'Types'},
                                         {'header': 'Number'}]}
)

chart = workbook.add_chart({'type': 'pie'})

chart.add_series({
    'name':       '=Sheet1!$A$1',
    'categories': '=Sheet1!$A$2:$A$4',
    'values':     '=Sheet1!$B$2:$B$4',
})

worksheet.insert_chart('D2', chart)

workbook.close()

Yes, it is the sample code from https://xlsxwriter.readthedocs.io/working_with_charts.html#working-with-charts

jsanchezf commented 9 months ago

I attach the resulting xlsx file generated from the sample code and that fails to load.

chart_pie.xlsx

The problem content is that in xl/drawings/drawing1.xml

jmcnamara commented 9 months ago

Thanks for the detailed report.

However, I can't reproduce it in 3 different versions of Excel that I tried including one that is close to yours:

Microsoft® Excel® for Microsoft 365 MSO (Version 2401 Build 16.0.17231.20194) 64-bit

screenshot

I get the same, successful, result with the file you attached.

jsanchezf commented 9 months ago

I can also read it with a 16.0.13901.20516 64 bits on a different computer. I have also succeeded with LibreOffice. It is weird message to show if it were a setting on my side. But you never can tell.

jmcnamara commented 9 months ago

Could you try the attached file. It was created from scratch in Excel 2007.

gh1044.xlsx

jmcnamara commented 8 months ago

Could you try the attached file. It was created from scratch in Excel 2007.

Any update on this?

jsanchezf commented 8 months ago

Sorry for not getting back to you earlier. I had this in the backburner waiting to do a system reboot and I did not notice your update. Your file loads cleanly but so does my original sample now. So the conclusion is that xlsxwriter is right, but may be triggering faults in Excel or the environment that only manifest in very particular circumstances. Unfortunately, I cannot ascertain what those circumstances might be. Some malware defense, maybe, though no trace of it has been found.

Thanks for your help.

Should I close the issue myself?

jmcnamara commented 8 months ago

Thanks for getting back to me with that information. It will be good to know in case it ever happens again.