Arabic PDF to Word, tested
25 of 25
paragraphs exact and right to left, typed PDFs
Arabic scan to text in one call
Typed PDFs: words mostly kept
3 typed Arabic PDFs to .docx, each file read back.
| Tool | Words found | Paragraphs exact, of 25 |
|---|---|---|
| LibreOffice Writer | 63.9% | 10 |
| pdf2docx | 95.1% | 12 |
| Tesseract, then pandoc | 87.5% | 8 |
| Our Actor, then pandoc | 97.1% | 20 |
| The script on this page | 97.1% | 25 |
- LibreOffice used 112 text boxes at fixed places, so lines do not follow a margin.
- LibreOffice stored 2026-1147 as 6202-7411, and the email backwards, in two of the 3 files.
- From the ReportLab PDF, LibreOffice kept presentation forms, so a search finds 0% of the words.
- pdf2docx found the words but not always in order: 83.5% of words in order.
Right to left is two pandoc options
-M dir=rtl -M lang=ar.
A .docx marks a right-to-left paragraph with w:bidi.
LibreOffice and pdf2docx set it on 0 paragraphs. pandoc sets it on
every one with these options.
pandoc in.md -o out.docx -M dir=rtl -M lang=ar
With them, 29 of 29 lines started at the right margin. Without them, 1 did.
What the options do, and what they do not
-
w:bidilays the paragraph out right to left: where lines start, and the alignment. -
The letters' order inside a line comes from the Unicode rules.
pandoc also marks each run
w:rtlandw:langar. - So the words must already be in typed order in the file. The options cannot turn a backwards line around.
Scans and broken fonts need OCR
Two more PDFs had a broken text layer: one font maps letters wrong, one lost its map.
| Tool | Words found, 2 scans | 2 broken layers |
|---|---|---|
| LibreOffice Writer | 0% | 0.3% |
| pdf2docx | 0% | 25.7% |
| Tesseract, then pandoc | 85.2% | 88.9% |
| OCRmyPDF, then pandoc | 84.9% | not run |
| Our Actor, then pandoc | 83% | 88.2% |
| The script on this page | 85.2% | 88.9% |
- LibreOffice and pdf2docx put each scan in as a picture.
- The script runs the same OCR steps, so its row matches.
- On the typed pages Tesseract kept 0 of 12 Arabic-Indic numbers. ١٧٩٨ became 1798; most were lost.
- Next to Arabic, 2026-1147 shows as 1147-2026, and Tesseract stored that in all 7 files.
The script: text first, then OCR
On each page it reads the PDF's own text and turns backwards lines around. A page with no usable Arabic goes to Tesseract. pandoc writes the .docx right to left.
pip install pdfplumber python-bidi
python arabic_pdf_to_word.py in.pdf out.docx
- You also need pandoc, and for scans and broken text layers Poppler and Tesseract with the ara model.
- A new paragraph starts where the line gap grows. A paragraph that runs onto the next page comes out as 2.
arabic_pdf_to_word.py, the whole script
"""Arabic PDF to Word: an editable .docx with right-to-left paragraphs. The script of brasspage.com/arabic-pdf-to-word/
pip install pdfplumber python-bidi (MIT; LGPL)
Also on your PATH: pandoc, and for scans and broken text layers Poppler (pdftoppm) and Tesseract with ara.
python arabic_pdf_to_word.py in.pdf out.docx
Each page: the PDF's own text, turned back to typed order if it was stored reversed. A page with no usable Arabic
text (a scan, or a font that maps letters wrong) is read by Tesseract. Lines become paragraphs where the line gap
grows, and pandoc writes every paragraph right to left (w:bidi) with every run marked w:rtl.
"""
import html
import re
import subprocess
import sys
import unicodedata
import pdfplumber
from bidi.algorithm import get_display
MARKS = dict.fromkeys(map(ord, ""))
def arabic_words(text):
return re.findall("[ء-ي]+", unicodedata.normalize("NFKC", text))
def typed_order(lines):
"""clean() of /extract-arabic-text-from-pdf/: reversed words end in لا more often than they start with ال."""
lines = [ln.translate(MARKS) for ln in lines]
words = arabic_words(" ".join(lines))
if sum(w.endswith("لا") for w in words) > sum(w.startswith("ال") for w in words):
lines = [get_display(ln, base_dir="R") for ln in lines]
return [unicodedata.normalize("NFKC", ln) for ln in lines]
def needs_ocr(lines):
words = arabic_words(" ".join(lines))
return len(words) < 20 or sum(len(w) == 1 for w in words) > 0.2 * len(words)
def from_text_layer(page):
lines = page.extract_text_lines(strip=True)
texts = typed_order([ln["text"] for ln in lines])
if not lines or needs_ocr(texts):
return None
gaps = [b["top"] - a["top"] for a, b in zip(lines, lines[1:])]
step = sorted(gaps)[len(gaps) // 2] if gaps else 0
size = sorted(ln["bottom"] - ln["top"] for ln in lines)[len(lines) // 2]
paras, cur = [], [texts[0]]
for gap, text in zip(gaps, texts[1:]):
if gap > step + size / 5: # extra space: a new paragraph. ponytail: none at a page break
paras.append(" ".join(cur))
cur = []
cur.append(text)
return paras + [" ".join(cur)]
def from_ocr(pdf, n):
"""pdftoppm's page image read better than pdfplumber's default one: 83.6 to 85.7% of words, against 74.3 to 80.8%."""
page = ["pdftoppm", "-r", "300", "-gray", "-png", "-f", str(n), "-l", str(n), pdf]
png = subprocess.run(page, capture_output=True, check=True).stdout
out = subprocess.run(["tesseract", "-", "-", "-l", "ara", "--dpi", "300"], input=png, capture_output=True, check=True)
blocks = re.split(r"\n\s*\n", out.stdout.decode())
return [" ".join(b.split()) for b in blocks if b.strip()]
def pdf_to_docx(pdf, docx):
paras = []
with pdfplumber.open(pdf) as doc:
for n, page in enumerate(doc.pages, 1):
paras += from_text_layer(page) or from_ocr(pdf, n)
body = "".join(f"<p>{html.escape(p)}</p>\n" for p in paras)
cmd = ["pandoc", "-f", "html", "-o", docx, "-M", "lang=ar", "-M", "dir=rtl"]
subprocess.run(cmd, input=body.encode(), check=True)
if __name__ == "__main__":
pdf_to_docx(sys.argv[1], sys.argv[2])
No OCR install: our Actor, then pandoc
Our Apify Actor, PDF to Markdown OCR, takes language ara. It read typed pages from their text, in typed order, and scans by OCR.
On the font that maps letters wrong it found the most words: 97.1%. On the scans it found a little fewer: 83% against 85.2%.
The API call, then pandoc
curl -s -X POST "https://api.apify.com/v2/acts/brasspage~pdf-ocr-to-markdown/run-sync-get-dataset-items" \
-H "Authorization: Bearer $APIFY_TOKEN" \
-H "Content-Type: application/json" \
-d '{"documentUrls": ["https://example.com/your-file.pdf"], "language": "ara"}' \
| jq -r '.[].markdown' > out.md
pandoc out.md -o out.docx -M dir=rtl -M lang=ar
- $0.006 per OCR page, plus $0.00005 a run at the default 1 GB. Pages read from their own text layer are free.
- Runs bill to your Apify account. Your files go to Apify's servers.
What I tested, and what I did not
- There is no Microsoft Word on this Mac. LibreOffice opened the files; Word was not tested.
- Online converters were not tested.
- Small test: 7 one-page PDFs from 2 book pages. Code made the scans.
How I tested
- Text: 2 proofread pages of 2 books on Arabic Wikisource, read at pinned revisions: a Moroccan law, and Zaydan's travel book, which has Arabic-Indic digits. Each got one made-up line with a date, an order number, an email, an amount and (PDF).
- Typed PDFs, one A4 page each, in the Amiri 1.003 font: Chrome 158 print to PDF, LibreOffice 26.8.1.1 export, and ReportLab 5.1.2 with arabic-reshaper and python-bidi, the common recipe.
- Broken text layers: Chrome with Noto Naskh Arabic 2.021, whose text layer maps letters wrong, and the Chrome Amiri PDF with its font map (ToUnicode) removed.
- Scans: the Chrome and LibreOffice pages at 300 dpi in gray, turned 0.4 degrees, blurred, with noise, saved as JPEG inside a PDF.
- Words found: the share of the page's distinct Arabic words, as typed, that a search finds in the .docx text. The mean over the files.
- Paragraphs exact: a .docx paragraph with the same Arabic letters as a typed paragraph. Right to left: w:bidi on the paragraph, its style or the file's defaults.
- No way found every word: the best found 99.3% on one book page and 96% on the other.
- LibreOffice: soffice --infilter=writer_pdf_import. pdf2docx 0.5.13 with PyMuPDF 2.2. Tesseract 5.5.4 with tessdata_best ara at 300 dpi. OCRmyPDF 17.13.0 --sidecar. pandoc 3.13.
- Our Actor: its page code from our repo, language ara, Markdown out, on this Mac. Not an Apify run.
- Lines: LibreOffice 26.8 opened the .docx files of the Chrome Amiri PDF and printed them; pdftotext gave where each line starts.
Sources
- w:bidi in the Open XML docs, which quote ISO/IEC 29500
- pdf2docx is MIT. It needs PyMuPDF, which is AGPL-3.0 or a paid license
- pdfplumber (MIT) and python-bidi (LGPL)
- tessdata_best ara for Tesseract (Apache-2.0)
- Fonts: Amiri and Noto Naskh Arabic, both OFL
- Scripts and results stay in our private repo, in research/bench/arabic-pdf-to-word-2026-11-27/.
- We make the Apify Actor, not these tools.
Questions people ask
How do I convert an Arabic PDF to Word for free?
Read the PDF's own text in typed order, then let pandoc write the .docx with -M dir=rtl. The free script on this page did that: 25 of 25 paragraphs exact and right to left, on 3 typed PDFs.
Why is my Arabic text backwards after PDF to Word?
Chrome and the common ReportLab recipe store Arabic lines in drawn order. LibreOffice kept the Arabic words in order but stored 2026-1147 as 6202-7411 in 2 of 3 files. pdf2docx kept 83.5% of words in order. Turn each line back with python-bidi first.
Can I convert a scanned Arabic PDF to Word?
Yes, with OCR first. LibreOffice and pdf2docx put the scan in as a picture, with 0 words. Tesseract with the ara model found 85.2% of the words on 2 scans made by code, and pandoc made the .docx.