Skip to content

Repository files navigation

PDF Style Analyzer

Python License: MIT

A Python script that uses the PyMuPDF library to analyze a PDF document and extract a wide range of formatting and style parameters through heuristics.

This project was born from the need to verify document consistency or quickly understand the style guide of a PDF without access to the original source document.

Important Note on Accuracy

PDF format is designed for visual presentation, not semantic structure. This script does not read "style" metadata, but infers parameters based on the position, size, and frequency of elements on the pages. The accuracy of the results will depend greatly on the consistency and structure of the analyzed document.


Extracted Parameters

The script attempts to identify the following document attributes:

1. Page Configuration

  • Paper Size: Page dimensions (e.g. 595.32 x 841.92 pts).
  • Orientation: Vertical or Horizontal.
  • Margins: Inferred blank space on the four edges.

2. Paragraph and Font

  • Body Font: Most common typeface in the document.
  • Font Size: Size in points of the body text.
  • Line Spacing: Estimated vertical space between lines (Single, 1.5, Double).
  • Alignment: Estimated text justification (Left, Justified).
  • First Line Indent: Indentation space at the start of a paragraph.
  • Paragraph Spacing: Estimated vertical space between paragraphs.

3. Titles and Headings

  • Heading Levels: Hierarchy of titles inferred by font size.
  • Heading Format: Font, size, and style (Bold/Normal) for each level.

4. Document Elements

  • Header and Footer: Content that repeats at the top and bottom of pages.
  • Page Numbering: Detection of page numbers in header or footer.

5. Figures and Tables

  • Legend Format: Heuristic detection of figure legend styles (e.g. "Figure 1: ...").

Installation

  1. Clone the repository:

    git clone https://github.com/your-user/PDFStyleAnalyzer.git
    cd PDFStyleAnalyzer
  2. (Recommended) Create a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies: The project depends on PyMuPDF. You can install it from the requirements.txt file.

    pip install -r requirements.txt

Usage Mode

  1. Place your PDF: Add the PDF file you want to analyze to the root of the repository or provide the full path.

  2. Run the script from the terminal with arguments:

    python main.py <input_pdf_path> <output_json_path>

    Example:

    python main.py example.pdf analysis_result.json
  3. Review the output: The script will print to the console a JSON object with all extracted parameters and save it to the specified output file.

    Example Output:

    {
        "1. Page Configuration": {
            "Paper Size": "595.28 x 841.89 pts",
            "Orientation": "Vertical",
            "Margins (inferred)": {
                "top": "72.00 pts",
                "bottom": "41.89 pts",
                "left": "72.00 pts",
                "right": "72.28 pts"
            }
        },
        "2. Paragraph and Font": {
            "Body Font": "Tinos-Regular",
            "Body Font Size": "12 pts",
            "Line Spacing (estimated)": "Single",
            ...
        },
        ...
    }

    And a message: "Analysis complete. Results saved to <output_json_path>"


License

This project is under the MIT License. See the LICENSE file for more details.

How to Contribute

Contributions are welcome! If you have ideas to improve the accuracy of the heuristics, add new features, or fix bugs, please open an issue or send a pull request.

About

A Python script that uses the PyMuPDF library to analyze a PDF document and extract a wide range of formatting and style parameters through heuristics.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages