High-quality text extraction from scanned PDFs with Persian/Arabic (RTL) support.
This project provides a powerful Python script that extracts text from PDFs โ especially scanned or image-based documents โ using Tesseract OCR, advanced image preprocessing, and robust text-cleaning tailored for Persian/Arabic languages.
It outputs both clean UTF-8 text and HTML with proper RTL support.
- Works with scanned PDFs
- Supports Farsi, Arabic, and English
- Runs multiple OCR configurations and selects the best output
- Uses OpenCV (if available):
- Adaptive thresholding
- Noise reduction
- Morphological cleanup
- Uses Pillow fallback when OpenCV is not installed
- Auto-detects Persian text
- Normalizes common OCR mistakes
- Fixes diacritics, punctuation, and spacing
- Converts digits contextually
- Uses arabic-reshaper + bidi for correct display
extracted_text_clean.txtextracted_text_clean.html- Page-separated output
pip install pytesseract PyMuPDF Pillow opencv-python arabic-reshaper python-bidi numpyWindows: https://github.com/UB-Mannheim/tesseract/wiki
Ubuntu :
sudo apt-get install tesseract-ocr tesseract-ocr-fasMacOs:
brew install tesseract
brew install tesseract-langPlace the PDF into the same directory and name it:
Sample.pdf
Run:
python pdf_ocr.py