# Export PDF Metadata to JSON Using 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.

## Install `pymupdf`

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

```python
%pip install -q -U pymupdf
```

## Import the dependencies

```python
import pymupdf
import json
from google.colab import files
```

## Upload the PDF

```python
uploaded = files.upload()
```

## Open the PDF

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

This opens the PDF so Python can access its information.

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

## 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
    

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

## Save the information as JSON

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

*   `"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.
    

## Download the JSON file

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

```python
files.download("pdf_information.json")
```

## Complete Example

```python
%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")
```

## Example Output

The resulting JSON might look like:

{  
    "page\_number": 4,  
    "metadata": {  
        "format": "PDF 1.5",  
        "title": "Artificial Intelligence (AI) ",  
        "author": "PDF Python Hub",  
        "subject": "An Overview of Artificial Intelligence, Machine Learning, Generative AI, Ethical Frameworks, and Future Outlook",  
        "keywords": "Artificial Intelligence, AI, Machine Learning, Deep Learning, Generative AI, Large Language Models, AI Ethics",  
        "creator": "Google Docs",  
        "producer": "Skia/PDF m153 Google Docs Renderer",  
        "creationDate": "D:20260816101230+02'00'",  
        "modDate": "D:20260818145823+02'00'",  
        "trapped": "",  
        "encryption": null  
    }  
}

* * *

## 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](https://colab.research.google.com/drive/19Kv37fgtdvSBh5G3x7e9l0_dBT1rJlvV?usp=sharing)\]

* * *

## More PDF Python Resources

Want to learn how to open a PDF file?

Check out this mini-guide:

[Open Your First PDF with PyMuPDF](https://payhip.com/PDFPythonHub/blog/news/open-your-first-pdf-with-pymupdf)
