DEV Community

Cover image for Convert PDF Data to JSON with Python
PDF Python Hub
PDF Python Hub

Posted on Originally published at payhip.com

Convert PDF Data to JSON with Python

The Python script below uses PyMuPDF to read basic information from a PDF, saves that information as a JSON file, and downloads the file to your computer.

1. Install pymupdf

If pymupdf isn't already installed in your Colab environment, run:

%pip install -q -U pymupdf
Enter fullscreen mode Exit fullscreen mode

2. Import the dependencies

import pymupdf
import json
from google.colab import files
Enter fullscreen mode Exit fullscreen mode

3. Upload the PDF

uploaded = files.upload()
Enter fullscreen mode Exit fullscreen mode

4. Open the PDF

doc = pymupdf.open("sample.pdf")
Enter fullscreen mode Exit fullscreen mode

This opens the PDF so Python can access its information.

Replace "sample.pdf" with the name of your PDF.

5. Collect the information

The script creates a dictionary containing:

  • page_number — the total number of pages
  • metadata — information such as the PDF title, author, subject, creator, and creation date
data = {
    "page_number": doc.page_count,
    "metadata": doc.metadata
}
Enter fullscreen mode Exit fullscreen mode

6. Save the information as JSON

with open("pdf_information.json", "w", encoding="utf-8") as f:
    json.dump(data, f, indent=4, ensure_ascii=False)
Enter fullscreen mode Exit fullscreen mode
  • "w" — opens the file in write mode, meaning new content is written to the file.
  • encoding="utf-8" — specifies UTF-8 character encoding, allowing the file to correctly handle characters from different languages and special characters.
  • as f — gives the opened file a short name (f).
  • json.dump() —writes Python data to JSON.
  • indent=4 — formats the JSON with 4 spaces of indentation, making it easier for humans to read.
  • ensure_ascii=False — keeps non-ASCII characters as they are instead of converting them into Unicode escape sequences. For example, é stays as é rather than becoming \u00e9.
  • with — automatically closes the JSON file when the block finishes, even if something goes wrong while writing.

7. Download the JSON file

Because this is running in Google Colab, you can download the generated file directly:

files.download("pdf_information.json")
Enter fullscreen mode Exit fullscreen mode

Complete Example

%pip install -q -U pymupdf

import pymupdf
import json
from google.colab import files

uploaded = files.upload()

doc = pymupdf.open("sample.pdf")

data = {
    "page_number": doc.page_count,
    "metadata": doc.metadata
}

with open("pdf_information.json", "w", encoding="utf-8") as f:
    json.dump(data, f, indent=4, ensure_ascii=False)

files.download("pdf_information.json")
Enter fullscreen mode Exit fullscreen mode

Example Output

The resulting JSON might look like:


Open the notebook

Open the Google Colab notebook for this PDF mini-guide and run the code as you follow along.

[Open in Google Colab]


To open a PDF file with PyMuPDF, check out this mini-guide: Open Your First PDF with PyMuPDF.

Top comments (0)