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

feature request: support webp image format #1006

Closed ChiaYen-Kan closed 1 year ago

ChiaYen-Kan commented 1 year ago

Feature Request

Hi

please support use webp image format in insert_image function

in version 3.1.2, if use insert_image function insert a webp image will got a excption

Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\kcy\.vscode\extensions\ms-python.python-2023.14.0\pythonFiles\lib\python\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "c:\Users\kcy\.vscode\extensions\ms-python.python-2023.14.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\kcy\.vscode\extensions\ms-python.python-2023.14.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\kcy\.vscode\extensions\ms-python.python-2023.14.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, 
in run_path
    return _run_module_code(code, init_globals, run_name,
  File "c:\Users\kcy\.vscode\extensions\ms-python.python-2023.14.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, 
in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\kcy\.vscode\extensions\ms-python.python-2023.14.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, 
in _run_code
    exec(code, run_globals)
  File "D:\ai\extract\main.py", line 231, in <module>
    workbook.close()
  File "C:\Users\kcy\AppData\Roaming\Python\Python310\site-packages\xlsxwriter\workbook.py", line 322, in close
    self._store_workbook()
  File "C:\Users\kcy\AppData\Roaming\Python\Python310\site-packages\xlsxwriter\workbook.py", line 702, in _store_workbook
    self._prepare_drawings()
  File "C:\Users\kcy\AppData\Roaming\Python\Python310\site-packages\xlsxwriter\workbook.py", line 1223, in _prepare_drawings
    ) = self._get_image_properties(filename, image_data)
  File "C:\Users\kcy\AppData\Roaming\Python\Python310\site-packages\xlsxwriter\workbook.py", line 1400, in _get_image_properties
    raise UnsupportedImageFormat(
xlsxwriter.exceptions.UnsupportedImageFormat: d:/ai\00001-20230830090038.webp: Unknown or unsupported image file format.

thank you

jmcnamara commented 1 year ago

The webp image format can't be supported by XlsxWriter because it isn't, strictly speaking, supported by Excel.

Recent versions of Excel allow you to import *.webp images but it converts them to *.png files on the fly and it doesn't store or render the webp file.

Here for example is an unzipped Excel file with a webp image (note that the media entry refers to a png file):

$ ./xunzip webp.xlsx
Archive:  webp.xlsx
  inflating: webp/[Content_Types].xml
  inflating: webp/_rels/.rels
  inflating: webp/xl/_rels/workbook.xml.rels
  inflating: webp/xl/workbook.xml
  inflating: webp/xl/drawings/drawing1.xml
 extracting: webp/xl/media/image1.png
  inflating: webp/xl/styles.xml
  inflating: webp/xl/drawings/_rels/drawing1.xml.rels
  inflating: webp/xl/theme/theme1.xml
  inflating: webp/xl/worksheets/sheet1.xml
  inflating: webp/xl/worksheets/_rels/sheet1.xml.rels
  inflating: webp/docProps/app.xml
  inflating: webp/docProps/core.xml

As such you will need to convert any webp images to a format that Excel (and XlsxWriter) supports like png or jpg.

Closing as can't/won't fix.