[Python] How to accurately extract multi-page answers (including text, formulas) with...

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a task involving a large number of PDF files. These PDFs contain answers to questions from various subjects like mathematics, physics, chemistry, statistics, etc. I need to extract each complete answer as an image or a set of images.

    The Challenges:

    Complex Content: The answers are not just plain text. They frequently contain complex mathematical formulas, graphs, charts, and other images. Variable Structure: The content layout varies significantly. Answers can be plain text paragraphs, structured in tables, or a mix. Inconsistent Numbering: The way answers are numbered is not uniform. It could be 1, 1., Q1, Q1., Answer 1, etc. The numbering might appear at the beginning of the line, indented, or elsewhere. Answer Boundaries: It's difficult to reliably determine where one answer ends and the next one begins, especially since: An answer can span multiple pages. A single page can contain multiple answers. Some pages might contain no answers at all. Answers often include sub-parts (e.g., a, b, c; bullet points) that should be considered part of the main answer. Tool Limitations: I've tried using Python's PyMuPDF (fitz) library to extract content. While it works reasonably well for simple text, it fails to correctly recognize or extract the complex mathematical formulas and graphical elements, often rendering them incorrectly or missing them. The Goal:

    For each logical answer (including all its text, formulas, images, graphs, and sub-parts):

    Identify the starting point and the ending point of the entire answer, even if it spans multiple pages. For each page that the answer appears on, determine the precise bounding box coordinates that encompass all parts of the answer present on that page. Generate a screenshot (an image file) of the area defined by the bounding box for each page the answer spans. (e.g., an answer covering parts of page 5 and page 6 should result in two separate image screenshots). Question:

    What are robust approaches, libraries (preferably Python-based), algorithms, or even external tools/APIs that can effectively handle these challenges? I'm looking for solutions that can:

    Parse complex PDF layouts. Handle or accurately recognize mathematical formulas and graphical elements within the flow. Reliably identify answer boundaries despite inconsistent numbering and structure. Manage multi-page answers. Ultimately provide the coordinates needed to take accurate screenshots of each answer segment per page. I'm open to suggestions involving OCR, computer vision, document layout analysis models, or any other relevant techniques. Example

    Example

    Continue reading...

Compartilhe esta Página