fix: detect headings in PDF conversion via font-size analysis#1659
Open
octo-patch wants to merge 2 commits intomicrosoft:mainfrom
Open
fix: detect headings in PDF conversion via font-size analysis#1659octo-patch wants to merge 2 commits intomicrosoft:mainfrom
octo-patch wants to merge 2 commits intomicrosoft:mainfrom
Conversation
…icrosoft#1640) PDFs with varied font sizes now produce Markdown headings. A new _extract_text_with_headings() function uses pdfminer's layout API to compare each text block's font size against the document's body-text size (mode). Blocks 15% or more larger are mapped to heading levels H1 through H6 (largest maps to H1). Plain-text PDFs now use this function instead of the raw pdfminer.high_level.extract_text() call. Falls back to pdfminer plain extraction on any error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1640
Problem
When converting PDFs (especially tagged or structured PDFs with multiple heading levels), MarkItDown produced flat plain text with no heading markers. Users lost the document structure — all text was rendered at the same level regardless of whether it was a heading or body text.
Solution
Added
_extract_text_with_headings()which uses pdfminer'sextract_pageslayout API to inspect per-character font sizes. The function:#…######(largest → H1).pdfminer.high_level.extract_text()on any error.Plain-text PDFs (no form/table pages detected by pdfplumber) now use
_extract_text_with_headings()instead of the barepdfminer.high_level.extract_text()call, so heading structure is preserved where font size information is available.Testing
test_pdf_masterformat.py,test_pdf_tables.py,test_pdf_memory.py.test_pdf_headings.pycovers:# …and## …markers.