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

Bug: .XlsxWriter throw UnsupportedImageFormat Exception even though image is PNG format #1061

Closed shalinis34 closed 7 months ago

shalinis34 commented 7 months ago

Current behavior

Currently throwing below error stderr workbook.close() stderr File "C:\Python39\lib\site-packages\xlsxwriter\workbook.py", line 351, in close stderr self._store_workbook() stderr File "C:\Python39\lib\site-packages\xlsxwriter\workbook.py", line 731, in _store_workbook stderr self._prepare_drawings() stderr File "C:\Python39\lib\site-packages\xlsxwriter\workbook.py", line 1252, in _prepare_drawings stderr ) = self._get_image_properties(filename, image_data) stderr File "C:\Python39\lib\site-packages\xlsxwriter\workbook.py", line 1429, in _get_image_properties raise UnsupportedImageFormat( stderr xlsxwriter.exceptions.UnsupportedImageFormat: excel/images/StanfordHealthCare.png: Unknown or unsupported image file format.

Expected behavior

It should write image into cell without throwing exception .

Sample code to reproduce

import xlsxwriter

workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()

   worksheet.insert_image('A1', filename,{'x_scale': x_scale, 'y_scale': y_scale})

workbook.close()

Environment

- XlsxWriter version:
- Python version:
- Excel version:
- OS:

Any other information

StanfordHealthCare

This PNG format image throwing unsupportedImageFormat exception

OpenOffice and LibreOffice users

jmcnamara commented 7 months ago

That isn't a PNG file. It just has a .png extension. It is actually a WebP file:

$ file gh1061.png
gh1061.png: RIFF (little-endian) data, Web/P image

$ xxd -l 64 gh1061.png
00000000: 5249 4646 3e6b 0000 5745 4250 5650 3858  RIFF>k..WEBPVP8X
00000010: 0a00 0000 1000 0000 af04 00f3 0100 414c  ..............AL
00000020: 5048 e32e 0000 01a0 55db 561d db9a 1290  PH......U.V.....
00000030: 8004 2420 2112 9010 0938 4002 1222 0109  ..$ !....8@.."..

If you convert it to an actual PNG file you should be able to insert in with XlsxWriter.

Note, Excel also doesn't support the WebP format and converts that format to PNG before importing them. See my previous analysis here: https://github.com/jmcnamara/XlsxWriter/issues/1006

Closing as "Can't fix".