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.
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.
The script attempts to identify the following document attributes:
- Paper Size: Page dimensions (e.g. 595.32 x 841.92 pts).
- Orientation: Vertical or Horizontal.
- Margins: Inferred blank space on the four edges.
- 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.
- Heading Levels: Hierarchy of titles inferred by font size.
- Heading Format: Font, size, and style (Bold/Normal) for each level.
- Header and Footer: Content that repeats at the top and bottom of pages.
- Page Numbering: Detection of page numbers in header or footer.
- Legend Format: Heuristic detection of figure legend styles (e.g. "Figure 1: ...").
-
Clone the repository:
git clone https://github.com/your-user/PDFStyleAnalyzer.git cd PDFStyleAnalyzer -
(Recommended) Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies: The project depends on
PyMuPDF. You can install it from therequirements.txtfile.pip install -r requirements.txt
-
Place your PDF: Add the PDF file you want to analyze to the root of the repository or provide the full path.
-
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
-
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>"
This project is under the MIT License. See the LICENSE file for more details.
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.