Urdu PDF to Word, tested
94.5%
whole words right on Naskh
Naskh: OCR it. Nastaliq: check it.
- Naskh: OCR it with Tesseract urd.
- Nastaliq: nothing we tested passed 56.9% of words. Check names and numbers against the PDF.
- Scanned: see Urdu OCR.
- Naskh letters sit on one flat line. Nastaliq words slope from upper right down to the left.
Whole words right, 5 Urdu PDFs
| Way | Nastaliq, Chromium | Nastaliq, LibreOffice | Naskh, Chromium | Broken | Scan |
|---|---|---|---|---|---|
| LibreOffice Writer | 3.2% | 8.2% | 7.3% | 0.0% | 0.0% |
| pdf2docx | 56.9% | 16.3% | 68.7% | 5.7% | not run |
| pdftotext, then pandoc | 39.1% | 35.8% | 53.2% | 27.0% | 0.0% |
| Tesseract urd (fast) | 39.1% | 35.7% | 94.5% | 39.1% | 25.8% |
| Tesseract urd best | 38.9% | 37.0% | 93.8% | 38.9% | 35.6% |
| Our Actor (Arabic model) | 4.2% | not run | 24.2% | not run | 6.4% |
| The script: text, OCR if broken | 39.1% | not run | 94.5% | 39.1% | 25.8% |
- Strict: Arabic ي, ك or ه for Urdu ی, ک or ہ counts wrong, and so does an Arabic presentation form (LibreOffice wrote 1,987 on Naskh). An exact search misses both.
- Broken looks like Nastaliq, Chromium: its Tesseract cells are copies.
Why converters break Urdu
- Words in pieces. The Nastaliq and Naskh PDFs' own text had the right letters, 92.3% to 98.3% in order, but split the words apart.
- In that text, one-letter words were 22.5% to 66.7% of a page, against 2.5% at most in the real text.
- Left to right. LibreOffice Writer and pdf2docx set right to left on 0 Urdu paragraphs. pandoc set it on every one.
- Text boxes. LibreOffice also split the typed pages into 415 to 4,486 text boxes per 13-page PDF.
Order numbers came back swapped
Each PDF has one made-up line with an order number, a date, an email and an amount.
- 2026-1147 came back in typed order in 0 of 28 .docx files.
- 19 files wrote it as 1147-2026, and 9 lost it.
- pdftotext already swapped it on the Nastaliq and Naskh PDFs.
- [email protected] stayed in 6 files, and in none made by OCR.
The script: text first, OCR if broken
It keeps a page's own text when it looks right and sends a broken page to Tesseract urd. pandoc writes the .docx right to left.
brew install tesseract tesseract-lang poppler pandoc
python3 urdu_pdf_to_word.py in.pdf out.docx
- Broken: under 20 Urdu letters, Private Use Area or NUL codes, 3 or more letters that are neither Arabic script nor A to Z, or over 10% one-letter words.
- On the 4 typed PDFs the rule sent all 52 pages to OCR, so the script was Tesseract urd and pandoc.
- For scans, pass a tessdata_best urd folder as a third argument.
- It marks the .docx Urdu (ur-PK) for complex script, not pandoc's Arabic (ar-SA).
urdu_pdf_to_word.py, the whole script
"""Urdu PDF to Word: an editable .docx with right-to-left paragraphs. The script of brasspage.com/urdu-pdf-to-word/
Needs on your PATH: Poppler (pdftotext, pdftoppm), Tesseract with the urd model, and pandoc. Python 3, stdlib only.
python3 urdu_pdf_to_word.py in.pdf out.docx [tessdata folder]
Each page: the PDF's own text (pdftotext), with the direction marks pdftotext adds taken out. A page whose own text
is broken is read by Tesseract (urd, 300 dpi) instead. Broken: fewer than 20 Urdu letters (a scan), Private Use
Area or NUL characters, 3 or more letters that are neither Arabic script nor A to Z (a font with no map back to
letters), or split words: over 10% of the Urdu words are one letter (real Urdu text has almost none; Nastaliq and
Naskh text layers copy as broken pieces). Give a tessdata folder that holds urd.traineddata if your Tesseract has no
urd, or to use the larger tessdata_best urd (better on scans). Blank lines split paragraphs. pandoc writes every
paragraph right to left, marks the text Urdu (ur), and set_urdu() makes the complex-script language ur-PK (pandoc
writes ar-SA). Each page's choice goes to stderr.
"""
import html
import re
import subprocess
import sys
import unicodedata
import zipfile
URDU = re.compile("[\u0620-\u063f\u0641-\u064a\u066e-\u066f\u0671-\u06d3\u06d5\u06ee\u06ef\u06fa-\u06fc\u06ff]")
WORD = re.compile("[\u0620-\u065f\u066e-\u06d3\u06d5\u06ee\u06ef\u06fa-\u06fc\u06ff]+")
MARKS = dict.fromkeys(map(ord, "\u061c\u200e\u200f\u202a\u202b\u202c\u202d\u202e\u2066\u2067\u2068\u2069"))
def broken(text):
"""Why a page's own text is broken, or None if it looks right."""
t = unicodedata.normalize("NFC", text)
if len(URDU.findall(t)) < 20: # ponytail: an English-only page goes to OCR too
return "no Urdu text"
if re.search("[\ue000-\uf8ff\x00]", t):
return "Private Use Area or NUL characters"
if sum(c.isalpha() and not ("\u0600" <= c <= "\u06ff" or c.isascii()) for c in t) >= 3:
return "letters from no script the page uses"
words = WORD.findall(t)
if sum(len(URDU.findall(w)) == 1 for w in words) > 0.1 * len(words):
return "split words"
return None
def ocr(pdf, n, tessdata):
page = ["pdftoppm", "-r", "300", "-gray", "-f", str(n), "-l", str(n), pdf] # PGM: no PNG packing (slow on scans)
img = subprocess.run(page, capture_output=True, check=True).stdout
cmd = ["tesseract", "-", "-", "-l", "urd", "--dpi", "300"]
if tessdata:
cmd += ["--tessdata-dir", tessdata]
r = subprocess.run(cmd, input=img, capture_output=True)
if r.returncode:
sys.exit(r.stderr.decode()) # Tesseract's own error, for example a missing urd model
return r.stdout.decode()
def paragraphs(text):
return [" ".join(b.split()) for b in re.split(r"\n\s*\n", text) if b.strip()]
def pdf_to_docx(pdf, docx, tessdata=None):
out = subprocess.run(["pdftotext", "-enc", "UTF-8", pdf, "-"], capture_output=True, check=True).stdout.decode()
paras = []
for n, text in enumerate(out.split("\f")[:-1], 1): # pdftotext ends every page with a form feed
text = text.translate(MARKS)
why = broken(text)
print(f"page {n}: " + (f"OCR ({why})" if why else "text layer"), file=sys.stderr)
paras += paragraphs(ocr(pdf, n, tessdata) if why else text) # ponytail: a paragraph across a page break stays two
body = "".join(f"<p>{html.escape(p)}</p>\n" for p in paras)
subprocess.run(["pandoc", "-f", "html", "-o", docx, "-M", "lang=ur", "-M", "dir=rtl"], input=body.encode(), check=True)
set_urdu(docx)
def set_urdu(docx):
"""pandoc's styles keep Arabic as the complex-script language (w:bidi="ar-SA"). Make it Urdu."""
with zipfile.ZipFile(docx) as z:
parts = [(info, z.read(info)) for info in z.infolist()]
with zipfile.ZipFile(docx, "w") as z:
for info, data in parts:
if info.filename == "word/styles.xml":
data = data.replace(b'w:bidi="ar-SA"', b'w:bidi="ur-PK"')
z.writestr(info, data)
if __name__ == "__main__":
if len(sys.argv) < 3:
sys.exit("usage: python3 urdu_pdf_to_word.py in.pdf out.docx [tessdata folder]")
pdf_to_docx(*sys.argv[1:4])
Our Actor reads Arabic, not Urdu
Our Apify Actor, PDF to Markdown OCR, reads scans in 23 languages. Arabic is one; Urdu is not.
- With Arabic (ara) it wrote ي and ك where Urdu has ی and ک: 234 and 269 times on the Naskh PDF.
- Even with ي, ك, ه and ى counted as right, it got 48.9% of words there.
- On Nastaliq, Chromium, it kept the PDF's own text on all 13 pages, so its Arabic model read none of them: 4.2% of words.
- So it does not fit Urdu PDFs. It fits Arabic ones: see Arabic PDF to Word.
What I tested, and what I did not
- Not tested: InPage files, Microsoft Word, Google Docs and online converters.
- No real-world Urdu PDF: only 5 PDFs I made from book text, in 2 Noto fonts.
- Other Tesseract settings moved Nastaliq words only to 36.1% to 42.1%, still under pdf2docx's 56.9%.
How I tested
- Text: 12 passages from proofread ur.wikisource pages, one a page, plus 1 made-up line. 1,000 words in all.
- 8 passages come from scans marked public domain on Commons and 4 from scans marked CC BY-SA 4.0. I quote no passage, only single letters.
- Words right: truth words found whole anywhere in the .docx text, with the same letters, each word counted as often as it appears. Harakat are not scored.
- PDFs of 13 pages: Chromium 153 print to PDF in Noto Nastaliq Urdu 4.000 and in Noto Naskh Arabic 2.021, and LibreOffice 26.8.1.1 export in Nastaliq (a few characters fell back to other fonts).
- Broken: the Chromium Nastaliq PDF with its 2 font maps (ToUnicode) removed. Scan: its pages at 300 dpi in gray, turned 0.4 degrees, blurred, with noise, as JPEG.
- Ways: LibreOffice Writer (soffice --infilter=writer_pdf_import), pdf2docx 0.5.13 with PyMuPDF 2.2, pdftotext from Poppler 26.03.0, Tesseract 5.5.4 at 300 dpi with tessdata_fast and tessdata_best urd 4.1.0, pandoc 3.13.
- Our Actor: its page code from our repo, language ara, on this Mac. Not an Apify run.
- Cut for time: pdf2docx on the scan, the Actor and the script on the LibreOffice PDF, and the Actor on the broken one.
- Second pass on Nastaliq: Tesseract urd at 200 to 600 dpi, page modes (psm) 3, 4 and 6 and tessdata_best on the Chromium PDF, psm 6 and tessdata_best on the LibreOffice one. Scored on the raw Tesseract text, which gives the same numbers as the .docx at 300 dpi.
- I scored the text inside each .docx, not how it looks.
Sources
- pdf2docx is MIT. It needs PyMuPDF, which is AGPL-3.0 or a paid license.
- tessdata_best and tessdata_fast for Tesseract (Apache-2.0).
- Fonts: Noto Nastaliq Urdu and Noto Naskh Arabic, OFL.
- Scripts and results stay in our private repo, in research/bench/urdu-pdf-to-word-2026-11-28/.
- We make the Apify Actor, not these tools.
Questions people ask
How do I convert an Urdu PDF to Word for free?
If it is typed in a Naskh font, OCR it with Tesseract urd and let pandoc write the .docx right to left. The free script on this page does both: 94.5% of words right on our Naskh PDF. On Nastaliq it got 39.1%. The best way there, pdf2docx, got 56.9% on one Nastaliq PDF and 16.3% on the other.
Why are my Urdu words broken into letters after converting?
In our typed test PDFs, the PDF's own text kept the letters but split the words, so a converter copied the pieces. OCR reads the page image and splits far fewer words, but on Nastaliq it misreads more letters.
Why does a search miss Urdu words in my .docx?
Some tools write the Arabic letters ي, ك or ه where Urdu uses ی, ک or ہ. They can look the same, but they are other characters, so an exact search for the Urdu word misses them. Tesseract's fast urd model wrote none on our 5 PDFs.