Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

2.4 KB
Raw Blame
"""Stop openpyxl from reading chartsheet drawings (images and charts).

``.xlsx`` text extraction goes unstructured ``partition_xlsx`` ->
``pandas.read_excel`` -> ``openpyxl.load_workbook(read_only=True)``.
Even in read-only mode openpyxl loads every chartsheet's drawing via
``openpyxl.reader.drawings.find_images``, which calls
``archive.read(target)`` once **per image reference** and keeps every
copy (and parses a chart part once per chart reference). One image
entry referenced N times therefore costs N times its size: a ~100 KB
upload can pin gigabytes, which no per-entry ceiling can bound.

Images and charts contribute no text, and this application uses
openpyxl only to extract text, so the reader's drawing hook is replaced
with one that returns nothing. That removes the per-reference reads at
their source instead of trying to count references in attacker-written
XML. (Worksheet drawings are only read outside read-only mode, which
this path never uses.)

The patch is **process-wide and permanent**: once the first ``.xlsx``
upload is processed, every openpyxl workbook loaded anywhere in this
process has no images or charts. Nothing else in the application reads
workbooks with openpyxl; anything added later that needs drawings must
load them some other way.
"""

from __future__ import annotations

from loguru import logger


def _no_drawings(archive, path):  # noqa: ARG001 - signature of find_images
    """Stand-in for ``openpyxl.reader.drawings.find_images``."""
    return [], []


def disable_openpyxl_drawing_reads() -> bool:
    """Replace openpyxl's drawing reader with a no-op (idempotent).

    Returns True when the hook is in place. Returns False, with a
    warning, when openpyxl is missing or no longer exposes the hook.
    The upload caller refuses .xlsx files in that case.
    """
    try:
        import openpyxl.reader.excel as excel_reader
    except ImportError:
        return False
    # Checked by identity rather than a flag, so a reload of openpyxl
    # (which restores the original hook) is patched again.
    if getattr(excel_reader, "find_images", None) is _no_drawings:
        return True
    if not hasattr(excel_reader, "find_images"):
        logger.warning(
            "openpyxl no longer exposes reader.excel.find_images; "
            ".xlsx drawings are read on load again"
        )
        return False
    excel_reader.find_images = _no_drawings
    return True