# Read and Extract Text from Scanned PDF Using Python OCR

Working with scanned PDFs can be frustrating, especially when you just want to copy a paragraph or analyze the content, but the file is an image. That’s where OCR (Optical Character Recognition) comes in. With Python, you can quickly turn hard-to-use scanned documents into searchable, editable text — all with just a few lines of code. In this guide, you’ll learn how to read and **extract text from scanned PDFs** using Python OCR in a simple, practical way.

## What Is a Scanned PDF and Why Do You Need OCR?

Before diving into extraction, it’s important to understand what a scanned PDF is and why OCR is necessary. Generally, there are two types of PDF files: editable PDFs—created in Word, Acrobat, or converted from other editable formats—and scanned PDFs, which are generated through scanners, cameras, or mobile photos. These scanned PDFs are basically images, meaning their text cannot be directly edited, copied, or searched.

So, if you want to extract text from a scanned PDF, organize the information, or reuse its content for further editing, you’ll need OCR. OCR converts the “image-based text” into actual machine-readable text, making the entire process efficient and hassle-free.

## Top Python Library for PDF Extraction

After understanding the basics of scanned PDFs, the next step is to prepare the right Python libraries for text extraction. In this guide, we’ll use Spire.PDF for Python and Spire.OCR for Python.

* *Spire.PDF* is a professional library designed for handling PDF documents. It allows developers to efficiently edit, convert, protect, and extract content from PDF files. In this article, we’ll use it mainly to convert scanned PDFs into images.
    
* *Spire.OCR* is a library dedicated to extracting text from images. Here, it will process the converted images and recognize the text within them.
    

You can install both libraries quickly using the commands below:

`pip install spire.pdf`

`pip install spire.ocr`

## Convert Scanned PDF to Images with Python

Since OCR cannot directly read text from a PDF file, the first step is to convert the scanned PDF into images. Fortunately, Spire.PDF makes this process extremely simple.

You just need to load the scanned PDF and loop through each page to ensure nothing is missed. If you only want to extract text from a specific page, you can skip the loop and target that page directly. Finally, call the `PdfDocument.SaveAsImage()` method to convert each page into PNG or JPG images.

Below is the complete code example.

```python
from spire.pdf import *

# Load the PDF file
pdf = PdfDocument()
pdf.LoadFromFile("/AI-Generated Art.pdf")

# Loop through pages and save as images
for i in range(pdf.Pages.Count):
    # Convert each page to image
    with pdf.SaveAsImage(i) as image:
        
        # Save in different formats as needed
        image.Save(f"/output/pdftoimage/ToImage_{i}.png")
        # image.Save(f"Output/ToImage_{i}.jpg")
        # image.Save(f"Output/ToImage_{i}.bmp")

# Close the PDF document
pdf.Close()
```

Here’s a preview of the converted images:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1763112432434/2c02f02b-3c3c-4c1f-816f-6283542ae1ef.png align="center")

## Extract Text from Scanned PDF Using OCR in Python

After converting the PDF pages into images, we can move on to text extraction. Spire.OCR is capable of accurately recognizing text in multiple languages—including English, Chinese, German, French, and more—and converting it into editable text.

Below is a code example that extracts text from an image and exports the result to a .txt file:

```python
from spire.ocr import *

# Create OCR scanner instance
scanner = OcrScanner()

# Configure OCR model path and language
configureOptions = ConfigureOptions()
configureOptions.ModelPath = r'E:/DownloadsNew/win-x64/'
configureOptions.Language = 'English'
scanner.ConfigureDependencies(configureOptions)

# Perform OCR on the image
scanner.Scan(r'/output/pdftoimage/ToImage_0.png')

# Save extracted text to file
text = scanner.Text.ToString()
with open('/output/scannedpdfoutput.txt', 'a', encoding='utf-8') as file:
    file.write(text + '\n')
```

Here’s the preview of the extracted text:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1763112459405/340ffd8e-2b61-477c-8a2f-0ef8c1946acf.png align="center")

## The Conclusion

By combining Spire.PDF and Spire.OCR, you can easily turn scanned PDFs into editable text using just a few lines of Python. Whether you’re organizing documents, processing data, or preparing searchable archives, this workflow keeps everything simple and efficient. Give it a try and streamline your PDF text extraction process.
