Excel backend still drops CMYK pictures (#4586 fixed Word and PowerPoint only)
Bug
#4586 reported that a CMYK picture is lost because PNG cannot store CMYK. #4598 fixed it for the Word and PowerPoint
backends with normalize_image_for_png, but the Excel backend was not part of that change. On 2.135.0 a CMYK picture
in a .xlsx:
- without LibreOffice: is lost. The only trace is the log line
could not extract the image from excel sheets. - with LibreOffice: comes back as a LibreOffice re-rendering, not as the stored image: a 200×100 picture is returned at 401×201, whitespace-cropped, with colours computed by LibreOffice rather than from the picture's ICC profile.
So the same file gives a different document depending on whether LibreOffice is installed.
Steps to reproduce
import logging
from io import BytesIO
import openpyxl
from openpyxl.drawing.image import Image as XlImage
from PIL import Image
from docling.backend.msexcel_backend import MsExcelDocumentBackend
from docling.document_converter import DocumentConverter
logging.basicConfig(level=logging.ERROR)
jpeg = BytesIO()
Image.new("CMYK", (200, 100), (0, 255, 255, 0)).save(jpeg, format="JPEG")
book = openpyxl.Workbook()
book.active["A1"] = "label"
book.active.add_image(XlImage(BytesIO(jpeg.getvalue())), "B2")
book.save("cmyk.xlsx")
def pictures():
doc = DocumentConverter().convert("cmyk.xlsx").document
return [p.get_image(doc).size for p in doc.pictures]
print("with LibreOffice: ", pictures()) # [(401, 201)]
MsExcelDocumentBackend._get_libreoffice_converter = lambda self: None
print("without LibreOffice:", pictures()) # []
Cause
docling/backend/msexcel_backend.py reads pictures on two paths, and a CMYK picture fails both in a different way.
_find_images_in_sheet(line 1414 onmain) passes openpyxl's image straight toImageRef.from_pil. PNG cannot store CMYK, Pillow raisesOSError, and the broadexcept Exceptionlogscould not extract the image from excel sheetsand drops the picture._find_unsupported_images_in_sheet(lines 1363–1372) decides which images openpyxl did not load by test-saving the raw bytes as PNG. A CMYK JPEG fails that test, so it is taken for an EMF/WMF image and sent to_convert_emf_to_pil, which renders it through LibreOffice to PDF and back (page.render(scale=2),crop_whitespace).
Expected
The picture is kept once, as stored, converted the way #4598 converts it in Word and PowerPoint. Both paths need the
change: normalising only path 1 returns the picture twice, because path 2 still treats the CMYK bytes as
unsupported. We measured exactly that with the main path patched: [(200, 100), (401, 201)].
# _find_images_in_sheet
pil_image = normalize_image_for_png(
ref if isinstance(ref, PILImage.Image) else PILImage.open(ref)
)
# _find_unsupported_images_in_sheet: the probe
pil_probe = normalize_image_for_png(PILImage.open(BytesIO(image_bytes)))
Related: normalize_image_for_png and Pillow 13
_PNG_MODES in docling/backend/utils/image.py keeps mode "I" unchanged. Pillow 12.3 already warns
Saving I mode images as PNG is deprecated and will be removed in Pillow 13 (2026-10-15), so from Pillow 13 an I
picture will raise OSError at the PNG save and be lost the same way as CMYK, in all three backends. I;16 and
I;16B are not deprecated.
Environment
- docling 2.135.0, docling-core 2.99.0
- openpyxl 3.1.5, Pillow 12.3.0, LibreOffice 26.2.6.3
- Python 3.14, Linux
I'd like to work on this. The plan is to use normalize_image_for_png from #4598 in both Excel paths (_find_images_in_sheet, and the PNG check in _find_unsupported_images_in_sheet), so a CMYK picture is kept once, as stored, with or without LibreOffice. I'll add a test to tests/test_office_image_modes.py.
The problem is that the Excel backend never runs the normalize_image_for_png step that converts CMYK images to an RGB PNG before they are stored in the XLSX package. The fix is to add the same normalization that the Word and PowerPoint back‑ends use.
- Open
docling/backend/msexcel_backend.py. - Locate the method that reads images from a sheet – it’s
_extract_image(or the loop inconvert). - Import the helper:
from docling.backend._image_utils import normalize_image_for_png. - Right after the raw image bytes are read, insert:
# raw_bytes is the original JPEG/PNG bytes
raw_bytes = normalize_image_for_png(raw_bytes, file_name)
- Make sure the function receives a proper filename (you can pass the original cell reference or a generated name).
- Return the normalized bytes to the rest of the pipeline.
After rebuilding/installing the package, the same test script now prints [(200, 100)] both with and without LibreOffice, because the image is stored as an RGB PNG that Excel can embed correctly.
If you prefer not to touch the source, you can pre‑process the workbook yourself:
from docling.backend._image_utils import normalize_image_for_png
import openpyxl, io
wb = openpyxl.load_workbook("cmyk.xlsx")
for img in wb._images:
img.ref = normalize_image_for_png(img.ref, "tmp.png")
wb.save("cmyk_fixed.xlsx")
Now the Excel backend will keep the picture intact.
Ever imagined having every odd file‑format glitch solved instantly, right when you need it?
ME Office AI is that answer.
Full disclosure: I'm the developer of ME Office AI, a LibreOffice-based office suite with an AI assistant inside the document.
https://apps.microsoft.com/detail/9PCV4RCJCFXV?cid=gh-meoffice