Skip to content

fix(ocr): keep spaces between columns on PDF pages that contain images - #2566

Open
psycmos wants to merge 1 commit into
microsoft:mainfrom
psycmos:fix/ocr-pdf-glued-columns
Open

psycmos wants to merge 1 commit into
microsoft:mainfrom
psycmos:fix/ocr-pdf-glued-columns

Conversation

@psycmos

@psycmos psycmos commented Sep 29, 2026

Copy link
Copy Markdown

Fixes #2565

On a PDF page that contains an embedded image and has OCR enabled, the page text was rebuilt from raw page.chars joined with "". Chars carry no separator between adjacent columns, so columns were glued together (Customer NameVendor Name Ltd).

This switches that block to page.extract_text_lines(strip=True, return_chars=False), which keeps the inter-word spacing. Each line's top is still the Y position used to interleave the image OCR blocks, so the interleaving is unchanged.

Tests

  • New: test_pdf_page_with_image_keeps_spaces_between_columns. It builds a page with two columns and a small image in memory (PyMuPDF is already a dependency), asserts the columns are not glued, and that the image is still OCR'd and interleaved. It fails on main.
  • Changed: test_pdf_complex_layout. Its snapshot encoded the bug: in that PDF Item (x 72–95) and Quantity (x 216–261) sit about 120 pt apart on one line, and were expected as ItemQuantity; Widget A5 becomes Widget A 5. Only those two tokens change. Headings, ordering and the OCR block position are identical.
  • pytest in packages/markitdown-ocr: 109 passed (108 before plus the new test), Python 3.11. black --check is clean on the two touched files.

Notes

On a page with an embedded image and OCR enabled, the page text was rebuilt
from raw page.chars joined with "", which glued adjacent columns together
("Customer NameVendor Name Ltd"). Use page.extract_text_lines() instead, which
keeps inter-word spacing. Line tops are still the Y positions used to
interleave the image OCR blocks.

Adds a regression test and updates the pdf_complex_layout snapshot, which had
the glued form ("ItemQuantity", "Widget A5") as its expected output.
@psycmos

psycmos commented Sep 29, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

San-Hsien (SanHsien) added a commit to SanHsien/markitdown that referenced this pull request Sep 30, 2026
…issue microsoft#2567

Adopt microsoft#2462, microsoft#2464, microsoft#2466, microsoft#2467, microsoft#2469, microsoft#2476 and microsoft#2477 via cherry-pick;
record remaining items as adoption pending or not applicable.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.

markitdown-ocr: columns are glued together on PDF pages that contain an embedded image

1 participant