Convert Arabic PDFs to text in Python
Extract Arabic text from scanned or image-only PDFs, with tashkeel and tables, using the open-source Nuqta OCR library.
1. Install
pip install "nuqta-ocr[vllm]" # Linux + NVIDIA GPU, fastest
pip install "nuqta-ocr[fast]" # transformers with fast kernels
pip install nuqta-ocr # transformers only
vLLM is the fastest backend (about 3 seconds per page for the 0.8B on a 24 GB GPU). Without a GPU, see running offline on CPU.
2. Read the PDF
from nuqta import OCR
if __name__ == "__main__":
ocr = OCR("nuqta-4b") # or "nuqta-0.8b"
result = ocr.read("contract.pdf", mode="markdown") # every page, rendered at 200 dpi
for i, page in enumerate(result.pages, 1):
print(f"--- page {i} ---")
print(page.text)
open("contract.md", "w", encoding="utf-8").write(result.text)
mode="markdown" keeps headings, lists and tables (as HTML). Use the default as-written mode for plain
text, or bare to remove all tashkeel.
3. Many files at once
results = ocr.read_many(["a.pdf", "b.pdf", "scan.jpg"], mode="markdown") # batched on the GPU
Command line
nuqta read contract.pdf --mode markdown > contract.md
Why not pdftotext?
Text extraction tools such as pdftotext or PyMuPDF only work when the PDF already contains a text layer. Scanned PDFs are images, and many Arabic PDFs have a broken text layer with letters in the wrong order or in presentation forms. OCR reads what is printed.
Questions
How do I extract Arabic text from a scanned PDF?
Use an OCR model, because a scanned PDF contains images, not text. With the open-source nuqta-ocr library: pip install nuqta-ocr, then OCR('nuqta-4b').read('file.pdf').text.
Why is Arabic text from my PDF reversed or broken?
The PDF's text layer stores Arabic in visual order or as presentation-form glyphs. OCR the rendered page instead of copying the text layer.