Malayalam PDF to Word, tested
98.1%
words right, 2 of 3 PDFs: the script
Whole words right, 3 typed PDFs
| Way | Chromium | LibreOffice | reportlab |
|---|---|---|---|
| LibreOffice Writer | 36.5% | 2.7% | 15.6% |
| pdf2docx | 78.2% | 74.9% | 13.3% |
| pdftotext | 97.7% | 98.1% | 15.4% |
| Tesseract: mal (fast) | 67.8% | 67.6% | 67.4% |
| The script: text, OCR if broken | 98.1% | 98.1% | 90.7% |
- pdftotext -raw split 7 of the 13 words with the old dot reph ൎ, as in ജൎമ്മ ൻ. Plain pdftotext split 8.
- Plain Tesseract mal typed 126 or 127 of the 130 chillus the old way.
Why converters break Malayalam
- Shapes, not words. LibreOffice Writer split lines into 131 to 277 text boxes. Vowel signs landed at the start of words.
- A weak copy. pdf2docx reads text with PyMuPDF. On the good PDFs, each kept 335 to 377 of 482 words.
- Private codes. The reportlab PDF's text holds 727 Private Use Area codes. Every copy of that text kept 15.6% of words or less.
The script: text first, OCR if broken
It keeps a good PDF's own text and OCRs a broken page with Tesseract mal.
python3 malayalam_pdf_to_word.py in.pdf out.docx
- It needs Python 3, pandoc, Poppler and Tesseract with mal.
- It copies with pdftotext -raw: 473 of 482 words on both good PDFs.
- Broken: under 20 Malayalam letters (a scan or an English page), Private Use Area or NUL codes, or 3 or more stray signs.
- On OCR pages it writes chillus as one letter and ൻറ as ന്റ, so searches find them.
- After that fix, the 3 models tested were within 2 points. It uses Homebrew's fast mal.
- It marks the .docx Malayalam (ml-IN), not pandoc's Arabic.
malayalam_pdf_to_word.py, the whole script
"""Malayalam PDF to Word: an editable .docx. The script of brasspage.com/malayalam-pdf-to-word/
Needs on your PATH: Poppler (pdftotext, pdftoppm), Tesseract with the mal model, and pandoc. Python 3, stdlib only.
python3 malayalam_pdf_to_word.py in.pdf out.docx [tessdata folder]
Each page: the PDF's own text (pdftotext -raw; -raw keeps the order the PDF stores the text in). A page whose own
text is broken is read by Tesseract (300 dpi) instead.
Broken: fewer than 20 Malayalam letters (a scan, or an old non-Unicode font like ML-TT that stores Latin codes),
Private Use Area characters, NUL characters, or 3 or more vowel signs or viramas that do not follow a consonant.
Give a tessdata folder that holds mal.traineddata if your Tesseract has no mal. On an OCR page the script makes two
fixes, so a search in Word finds the word: Tesseract types most chillus the old way (consonant, virama, U+200D),
and the script writes the one-letter chillu (ൺ ൻ ർ ൽ ൾ ൿ); it writes ൻറ as ന്റ. It also deletes a zero width
non-joiner (U+200C) at the end of a word and keeps the ones inside a word. Blank lines split paragraphs. pandoc
writes the .docx in Malayalam (ml-IN), and set_malayalam() marks it Malayalam for complex script too (ml-IN, not
pandoc's ar-SA). Each page's choice goes to stderr.
"""
import html
import re
import subprocess
import sys
import unicodedata
import zipfile
MALAYALAM = re.compile("[\u0d00-\u0d4e\u0d54-\u0d57\u0d5f-\u0d63\u0d7a-\u0d7f]") # Malayalam letters and signs
ORPHAN = re.compile( # a sign not after a consonant (a virama after the u sign, the old samvruthokaram, is fine)
"(?<![\u0d15-\u0d3a])[\u0d3e-\u0d4c\u0d57\u0d62\u0d63]|(?<![\u0d15-\u0d3a\u0d41])\u0d4d"
)
OLD_CHILLU = re.compile("([\u0d23\u0d28\u0d30\u0d32\u0d33\u0d15])\u0d4d\u200d") # ണ ന ര ല ള ക + virama + ZWJ
CHILLU = dict(zip("\u0d23\u0d28\u0d30\u0d32\u0d33\u0d15", "\u0d7a\u0d7b\u0d7c\u0d7d\u0d7e\u0d7f"))
OLD_NTA = re.compile("\u0d7b([\u0d46\u0d47\u0d48]?)\u0d31") # ൻറ, ൻെറ: the old way to type ന്റ, ന്റെ
END_ZWNJ = re.compile("\u200c(?![\u0d00-\u0d7f])") # a zero width non-joiner with no Malayalam letter after it
def broken(text):
"""Why a page's own text is broken, or None if it looks right."""
t = unicodedata.normalize("NFC", text).translate({0x200C: None, 0x200D: None})
if len(MALAYALAM.findall(t)) < 20: # ponytail: an English-only page goes to OCR too
return "no Malayalam text"
if re.search("[\ue000-\uf8ff]", t):
return "Private Use Area characters"
if "\x00" in t:
return "NUL characters"
if len(ORPHAN.findall(t)) >= 3: # one stray sign is a typing slip, not a broken page
return "vowel signs or viramas not after a letter"
return None
def fix(text):
"""Old chillus to one-letter chillus, ൻറ to ന്റ, no zero width non-joiner at a word end."""
t = OLD_CHILLU.sub(lambda m: CHILLU[m[1]], unicodedata.normalize("NFC", text))
return END_ZWNJ.sub("", OLD_NTA.sub("\u0d28\u0d4d\u0d31\\1", t))
def ocr(pdf, n, tessdata):
page = ["pdftoppm", "-r", "300", "-gray", "-png", "-f", str(n), "-l", str(n), pdf]
png = subprocess.run(page, capture_output=True, check=True).stdout
cmd = ["tesseract", "-", "-", "-l", "mal", "--dpi", "300"]
if tessdata:
cmd += ["--tessdata-dir", tessdata]
r = subprocess.run(cmd, input=png, capture_output=True)
if r.returncode:
sys.exit(r.stderr.decode()) # Tesseract's own error, for example a missing mal model
return fix(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", "-raw", "-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
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=ml-IN"], input=body.encode(), check=True)
set_malayalam(docx)
def set_malayalam(docx):
"""pandoc's styles keep Arabic as the complex-script language (w:bidi="ar-SA"). Make it Malayalam."""
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="ml-IN"')
z.writestr(info, data)
if __name__ == "__main__":
if len(sys.argv) < 3:
sys.exit("usage: python3 malayalam_pdf_to_word.py in.pdf out.docx [tessdata folder]")
pdf_to_docx(*sys.argv[1:4])
What OCR still misses
- None of the 5 Tesseract Malayalam models has ൎ. The OCR got 0 of the 13 words that hold it.
- It got 1 of the 6 words that end in ു്, as in ഇതു്.
Scanned pages: the same fix
Real scans read worse than typed pages. On 29 scans in our Malayalam OCR test, the fast mal read 89.9% of letters, against about 97% here.
There, the same two chillu rules lifted whole words from 42.1% to 55.3%.
Our Actor: no install, no Malayalam yet
Our Apify Actor, PDF to Markdown OCR, turns PDFs into Markdown in one call. It reads 23 languages, not Malayalam yet.
- Runs bill to your Apify account. Your files go to Apify's servers.
What I tested, and what I did not
- No Microsoft Word, no online converter, no ML-TT or other old non-Unicode Malayalam font.
- Typed PDFs only, plus the scan numbers above.
How I tested
- Text: 12 passages from proofread ml.wikisource pages, one a page. 10 are public domain, 1 is CC BY-SA 2.5 and 1 has no license tag. I quote no passage, only single words.
- They hold 482 words, 738 viramas and 130 one-letter chillus.
- PDFs in Noto Sans Malayalam 2.104: Chromium 153 print to PDF and LibreOffice 26.8.1.1 export, both with lang ml, and reportlab 5.1.2 with shaping on.
- 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 mal and script/Malayalam and tessdata_best mal 4.1.0, pandoc 3.13.
- Words right: truth words found whole in the .docx text, in any order, each word counted as often as it appears. Text from pdftotext and Tesseract went to pandoc for the .docx.
- I scored the text inside each .docx. LibreOffice on this Mac drew no Malayalam when it printed the 4 .docx files made from the Chromium PDF, so I did not check how they look.
- Payyans, a free ML-TT to Unicode converter, was not run: its PyPI release needs libindic-normalizer, which is not on PyPI.
- Our Actor has no Malayalam model, so it was not in this test.
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).
- Font: Noto Sans Malayalam, OFL.
- Scripts and results stay in our private repo, in research/bench/malayalam-pdf-to-word-2026-11-28/.
- We make the Apify Actor, not these tools.
Questions people ask
How do I convert a Malayalam PDF to Word for free?
Keep the PDF's own text where it is right, and OCR the other pages with Tesseract mal. The free script on this page does both, then pandoc writes the .docx: 473 of 482 words on 2 typed PDFs, 437 on a third that needed OCR.
Why does LibreOffice break Malayalam text from a PDF?
Its PDF import splits lines into text boxes, 131 to 277 on our 3 PDFs, and puts vowel signs at the start of words. It kept 2.7% to 36.5% of words.
Why do searches miss words in my Malayalam OCR text?
Tesseract types most chillus as a consonant, a virama and U+200D, not as one letter like ൻ. Tesseract mal wrote 126 or 127 of 130 that way. The script rewrites them as one letter.