pymupdf / PyMuPDF

PyMuPDF is a high performance Python library for data extraction, analysis, conversion & manipulation of PDF (and other) documents.
https://pymupdf.readthedocs.io
GNU Affero General Public License v3.0
5.66k stars 528 forks source link

Prevent line breaks, deliver reading order. #3878

Closed JorjMcKie closed 1 month ago

JorjMcKie commented 1 month ago

Refactor plain text and "words" extraction when option sort=True is used: We previously simply sorted output by ascending bottom and left coordinate. This change collects words (and respectively text portions) that are approximately on the same line. Apart from extremely malformed pages, words and text are returned in "natural" reading sequence.

This change also suppresses line breaks generated by MuPDF just because of large horizontal distances, as it e.g. often happens between table cell content of the same row.

This change does not alter the user's API and is only activated if Page.get_text(sort=True) or Page.get_text("words", sort=True) is used.

JorjMcKie commented 1 month ago

Have added test data and script.

JorjMcKie commented 1 month ago

Now verified that it works for MuPDF v1.24.9 ...