This script allows you to extract text from PDF documents using either direct text extraction or Optical Character Recognition (OCR). It's especially useful for scanned PDFs or image-based documents that don't contain selectable text.
- Attempts fast text extraction using PyMuPDF
- Falls back to OCR using Tesseract if needed
- Converts pages to images at 2x zoom for better OCR accuracy
- Saves output as
.docx(Word) or.txt(plain text) - Offers a user-friendly command-line interface
- Prints progress with helpful messages per page
Before running the script, ensure you install the following:
- Go to: https://github.com/UB-Mannheim/tesseract/wiki
- Download and install the latest version (e.g.,
tesseract-ocr-w64-setup-5.3.4.20240606.exe)
💡 After installation, the script attempts to auto-detect Tesseract on Windows. No need to configure paths manually unless necessary.
Use pip to install required libraries:
pip install PyMuPDF pillow pytesseract python-docxYou can run the script via command line:
python pdf_text_extractor.py path/to/your/file.pdfIf no file path is provided as an argument, you will be prompted to input it.
After extraction, choose your preferred format:
- Word document (
.docx) - Plain text (
.txt) - Both
- Simple extraction is attempted using PyMuPDF's native
get_text()method. - If no text is found:
- The script falls back to OCR using Tesseract.
- Each page is rendered as a high-resolution PNG and passed through Tesseract for text recognition.
- You are prompted to save the results in the desired format.
Output files will be saved in the same directory as your input PDF:
yourfile_extracted.docxyourfile_extracted.txt
- OCR can be slower and depends on the quality of the scanned text.
- For best results, make sure your PDF pages contain high-resolution scans.
MIT License
Made with ❤️ by Denis (BeforeMyCompileFails) — 2025