The error message indicates that the pdf2image library is unable to find the pdfinfo tool, which is part of the Poppler utilities. This is a prerequisite for pdf2image to convert PDF files into images.
Here are the steps to resolve the issue:
1. Install Poppler
For Windows:
- Download the Poppler for Windows binaries from this link. Look for the latest zip file.
- Extract the downloaded zip file to a directory, for example,
C:\Program Files\poppler-xx_xx_xx\ (where xx_xx_xx is the version number).
- Inside the extracted folder, locate the
bin folder which contains the Poppler executables.
2. Add Poppler to the System PATH
To ensure that pdf2image can find the pdfinfo command:
- Right-click on "This PC" or "My Computer" on your desktop or file explorer and select "Properties."
- Click on "Advanced system settings."
- Click on the "Environment Variables" button.
- In the "System variables" section, find the
Path variable, select it, and click "Edit."
- Click "New" and add the path to the
bin folder where you placed Poppler (e.g., C:\Program Files\poppler-xx_xx_xx\bin).
- Click "OK" to close all dialog boxes and apply the changes.
3. Verify Installation
- Open a new Command Prompt window (this is important to ensure the updated PATH is recognized).
- Type
pdfinfo -v and hit Enter. If Poppler is installed correctly, it should return the version of the pdfinfo tool.
4. Update the Code to Include the Language Parameter (Optional)
Since your original PDF contains Russian text, you should specify the language parameter to Tesseract when performing OCR:
text = pytesseract.image_to_string(image, lang='rus')
Make sure you have the Russian language data for Tesseract installed. You can do this by adding the language file (usually named rus.traineddata) to the Tesseract tessdata directory, which is typically found in the Tesseract installation directory.
Full Updated Code Example
Here is your updated code with the language parameter included:
import pytesseract
from pdf2image import convert_from_path
# Path to your Tesseract executable
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
def pdf_to_text(pdf_path):
# Convert PDF to images
images = convert_from_path(pdf_path)
# Initialize an empty string to hold the extracted text
extracted_text = ""
# Iterate through each image
for i, image in enumerate(images):
# Use pytesseract to do OCR on the image, specify Russian language
text = pytesseract.image_to_string(image, lang='rus')
extracted_text += f"--- Page {i + 1} ---\n{text}\n"
return extracted_text
# Specify your PDF file path
pdf_path = r"C:\Users\user\Downloads\out\output.pdf"
text = pdf_to_text(pdf_path)
# Print the extracted text
print(text)
# Optionally, save the extracted text to a file
with open(r"C:\Users\user\Downloads\out\extracted_text.txt", 'w', encoding='utf-8') as f:
f.write(text)
Final Remarks
Once you finish these steps, try running your code again. It should now work without the previous errors, and it should also extract the Russian text from your PDF file correctly. If you encounter any more issues, feel free to ask for further assistance!