Skip to content

Refactor Office OCR converters to reuse core conversion pipelines - #2506

Merged
afourney merged 6 commits into
mainfrom
ocr_refactor
Sep 16, 2026
Merged

afourney merged 6 commits into
mainfrom
ocr_refactor

Conversation

@afourney

@afourney afourney commented Sep 15, 2026

Copy link
Copy Markdown
Member

Summary

Replace the OCR plugin’s duplicated DOCX, PPTX, and XLSX conversion pipelines with thin subclasses of the core converters. Native parsing and formatting fixes now benefit OCR-enabled conversion automatically, rather than requiring parallel fixes in the plugin.

Each core Office converter exposes the same semi-private _image_to_html(image_stream, stream_info, **kwargs) hook. Core handles image extraction and placement; the plugin supplies OCR text as escaped HTML for the shared HTML-to-Markdown renderer.

  • DOCX inserts image fragments before Markdown conversion, preserving table/list context and splitting enclosing paragraphs where needed.
  • PPTX inherits core slide traversal, tables, charts, notes, and image resolution while retaining caption-first behavior.
  • XLSX reads images from the repaired workbook, validates drawing/image relationship types, and preserves the existing images-after-table layout and image ordering.
  • OCR recognition is cached per conversion, with results reused at each image occurrence.
  • Image metadata reaches compatible OCR services, preserving MIME types such as image/svg+xml, while legacy one-argument services remain supported.

Compatibility and output

Public constructor and conversion signatures are unchanged. Hooks that return None or blank text retain native rendering.

OCR markers are preserved, but recognized text now follows shared Markdown escaping and line-break handling. PPTX also inherits core’s real newlines and empty-title/notes handling; XLSX heading spacing follows core.

Bump core to 0.1.8b3 and require markitdown>=0.1.8b3 in the OCR package so dependency resolution enforces hook availability.

PDF and legacy .xls conversion are unchanged. SVG rasterization and broader HTML-table formatting improvements remain outside this PR.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The OCR package dependency permits older core versions that lack the newly required hook.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Adds a reusable DOCX image-to-HTML hook and refactors DOCX OCR to use the core conversion pipeline.

Changes:

  • Introduces image fragment replacement with native fallback.
  • Simplifies DOCX OCR through inheritance and per-document caching.
  • Adds comprehensive core and OCR integration tests.
File summaries
File Description
packages/markitdown/src/markitdown/converters/_docx_converter.py Adds the image-rendering hook.
packages/markitdown/src/markitdown/converter_utils/docx/_images.py Bridges image callbacks into generated HTML.
packages/markitdown/tests/test_docx_images.py Tests hook behavior and HTML placement.
packages/markitdown-ocr/src/markitdown_ocr/_docx_converter_with_ocr.py Refactors OCR onto the core converter.
packages/markitdown-ocr/tests/test_docx_inheritance.py Tests inherited OCR conversion behavior.
packages/markitdown-ocr/tests/test_docx_converter.py Updates OCR output expectations.
Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/markitdown-ocr/src/markitdown_ocr/_docx_converter_with_ocr.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The hook, fallback behavior, error handling, compatibility constraint, and OCR integration are comprehensively tested.

Review details
  • Files reviewed: 9/9 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

XLSX relationship validation can expose non-image package parts to OCR, and whitespace-only PPTX captions incorrectly suppress OCR fallback.

Get a fresh assessment by requesting another Copilot review.

Review details
  • Files reviewed: 21/21 changed files
  • Comments generated: 2
  • Review effort level: Balanced

Comment thread packages/markitdown/src/markitdown/converter_utils/_xlsx_images.py Outdated
Comment thread packages/markitdown/src/markitdown/converters/_pptx_converter.py
afourney and others added 2 commits September 16, 2026 07:50
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Office OCR calls discard image MIME metadata, causing formats such as SVG to be submitted with an incorrect media type.

Get a fresh assessment by requesting another Copilot review.

Review details

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

packages/markitdown-ocr/src/markitdown_ocr/_docx_converter_with_ocr.py:54

  • Discarding the image StreamInfo forces the bundled OCR service to infer the MIME type from Pillow. DOCX can contain formats such as SVG that Pillow does not identify, causing valid SVG bytes to be submitted as data:image/png. Please pass metadata to services that support it while preserving the documented one-argument custom-service compatibility.
  • Files reviewed: 21/21 changed files
  • Comments generated: 2
  • Review effort level: Balanced

Comment thread packages/markitdown-ocr/src/markitdown_ocr/_pptx_converter_with_ocr.py Outdated
Comment thread packages/markitdown-ocr/src/markitdown_ocr/_xlsx_converter_with_ocr.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The broad Office conversion and plugin refactor warrants final human validation despite extensive automated coverage.

Review details
  • Files reviewed: 23/23 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@afourney afourney changed the title Add _image_to_html helper for docx ocr conversion. Refactor Office OCR converters to reuse core conversion pipelines Sep 16, 2026
@afourney
afourney merged commit 945314a into main Sep 16, 2026
24 checks passed
@afourney
afourney deleted the ocr_refactor branch September 16, 2026 17:23
Lukas (L4XB) added a commit to L4XB/markitdown that referenced this pull request Sep 17, 2026
microsoft#2506 turned `_read_xlsx_sheets` into a context manager and made
`XlsxConverterWithOCR` a subclass of `XlsxConverter`. The fix now sits on
that structure: both `read_excel` calls in `_read_xlsx_sheets` pass
`dtype=object`, and the sheet loop in `XlsxConverter.convert` renders
through `sheet_to_html`.

`_xlsx_converter_with_ocr.py` is taken from main as is. It no longer reads
or renders a sheet itself, so the fix reaches it through the parent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants