How to OCR a scanned Arabic PDF into editable Word text

2026-12-06来源 dev.toEnglish

A scanned Arabic PDF has no text in it, only pictures of pages, so copying, searching or editing it needs OCR (optical character recognition). The short version: check that the PDF really has no text layer, turn each page into a 300 DPI image, run an OCR engine that supports Arabic, then put the text into a Word file with right-to-left paragraphs.

Below is each step with commands you can run, plus the things that usually go wrong with Arabic.

Step 1: check if the PDF already has text

Many "scanned-looking" PDFs actually have a text layer. If yours does, you don't need OCR, you need text extraction (and maybe a fix for reversed words, which I covered in an earlier post).

Two quick checks with poppler-utils:

pdffonts scan.pdf # an empty font list = pages are images only pdftotext scan.pdf - | wc -c # ~0-1 characters = no text layer 

If pdffonts lists no fonts and pdftotext returns nothing, the PDF is image-only. Go to step 2.

Step 2: turn the pages into images

OCR engines work on images. Render every page at 300 DPI, which is a good default for printed text:

pdftoppm -r 300 -png scan.pdf page # -> page-1.png, page-2.png, ... 

Lower resolutions lose the small dots that tell Arabic letters apart (ب ت ث, ج ح خ), which is the most common source of wrong letters.

Step 3: run OCR with Arabic

Option A: Tesseract (free, open source)

Install Tesseract and its Arabic language data (ara). On Debian/Ubuntu the data package is tesseract-ocr-ara. Then:

tesseract page-1.png page-1 -l ara+eng # writes page-1.txt 

ara+eng lets it read English words and Latin digits inside Arabic lines. I tested this on a fictional scanned page (a slightly rotated image-only PDF) and the body text came back correctly, including the number 1250 and the year 2024. The bold heading at the top was skipped, which is a good reminder to check every page by eye.

For many pages, loop over them:

for f in page-*.png; do tesseract "$f" "${f%.png}" -l ara+eng; done cat page-*.txt > all.txt 

Option B: a cloud OCR service

Cloud OCR APIs (for example Azure AI Document Intelligence or Google Cloud Vision) list Arabic among their supported languages and usually do better than Tesseract on phone photos, low-contrast scans and mixed layouts. They cost money per page and you upload the document to the provider, so check their pricing and data terms first.

Whatever you pick, test it on your own real documents. Results for Arabic vary a lot with fonts, scan quality and layout.

Step 4: put the text into Word, right-to-left

If you paste Arabic into a Word paragraph that is set to left-to-right, punctuation and numbers end up on the wrong side. Set the paragraph direction to RTL. With python-docx:

import sys from docx import Document from docx.oxml import OxmlElement def rtl_paragraph(doc, text): p = doc.add_paragraph() p._p.get_or_add_pPr().append(OxmlElement("w:bidi")) # paragraph is RTL run = p.add_run(text) run._r.get_or_add_rPr().append(OxmlElement("w:rtl")) # run is RTL text return p doc = Document() # Tesseract separates paragraphs with a blank line for block in open(sys.argv[1], encoding="utf-8").read().split("\n\n"): if block.strip(): rtl_paragraph(doc, " ".join(block.split())) doc.save(sys.argv[2]) 
python3 todocx.py all.txt result.docx 

This gives you editable text. It does not rebuild the original layout: tables, columns, images and bold headings are not recreated. If you need tables, use a tool that detects tables and exports them to Excel or CSV instead.

What usually goes wrong with Arabic OCR

  • Dots and similar letters. Low resolution or blur turns ث into ت or ن. Fix: 300 DPI, sharp photos, no shadows.
  • Tashkeel (diacritics). Harakat are small and often dropped or misplaced. If you need them exactly (Quran, poetry, teaching material), proofread carefully.
  • Mixed Arabic and English. Numbers and Latin words inside a line can come out in the wrong position. Use ara+eng and check those lines.
  • Two columns. Some engines read across both columns. Crop the columns first, or use a layout-aware OCR service.
  • Handwriting. Much harder than print. Test before you rely on it.
  • Tilted phone photos. Straighten (deskew) and crop to the page before OCR.

FAQ

Can I just copy the text out of the scanned PDF?
No. An image-only PDF has no characters to copy. You need OCR first.

Is Tesseract good enough for Arabic?
For clean printed scans, often yes. For phone photos, unusual fonts or complex layouts, a cloud OCR service usually does better. Test both on your documents.

Will the Word file look like the original page?
Not with this method. You get the text in RTL paragraphs, not the layout, tables or images.

A ready-made option

If you'd rather not set this up, I build MyPDFX, an Arabic-first PDF tool that does these steps for you: upload a scanned Arabic PDF and get the text plus a right-to-left Word file. The interface is in Arabic, a free account gets 3 pages a month, and like the script above it gives you the text, not the original layout.

For the related problem of reversed or broken letters when copying text from a PDF, there's a small free MIT helper: github.com/mypdfx/arabic-pdf-text-fix.

Disclosure: I build MyPDFX, an Arabic-first PDF tool.

原文:https://www.a8q9.cn/en/article/how-to-ocr-a-scanned-arabic-pdf-into-editable-word-text-e9e05b.html

相关文章