Gujarati PDF to Word, tested
99.9%
words right, 2 of 3 PDFs: the script
Whole words right, 3 typed PDFs
| Way | Chromium | LibreOffice | reportlab |
|---|---|---|---|
| LibreOffice Writer | 58.7% | 6.2% | 57.3% |
| pdf2docx | 83.9% | 99.0% | 0.0% |
| pdftotext | 99.7% | 99.5% | 61.4% |
| Tesseract: guj (fast) | 99.4% | 99.5% | 99.1% |
| The script: text, OCR if broken | 99.9% | 99.9% | 99.1% |
- Plain pdftotext put a space inside 4 words on the Chromium PDF, as in કૂં પા.
- Tesseract misread બુઢ્ઢીની and dropped the ં of ભર્યાં on all 3 PDFs.
Why converters break Gujarati
- Shapes, not words. LibreOffice Writer split lines into 359 to 862 text boxes. Vowel signs landed at the start of words.
- A weak copy. pdf2docx reads text with PyMuPDF, whose plain copy kept 1,092 and 1,203 of 1,211 words on the good PDFs.
- Private codes. The reportlab PDF's text holds 565 Private Use Area codes. pdf2docx skipped all 12 of its pages: "All strings must be XML compatible".
The script: text first, OCR if broken
The script keeps a good PDF's own text and sends a broken page to Tesseract guj.
python3 gujarati_pdf_to_word.py in.pdf out.docx
- It needs Python 3, pandoc, Poppler and Tesseract with guj.
- It copies with pdftotext -raw, which split just 1 word.
- Broken means under 20 Gujarati letters (a scan or an English page), Private Use Area or NUL codes, or 3 or more stray signs.
- Homebrew's fast guj read as many words as guj best and the Sauvola threshold, or more.
- It marks the .docx Gujarati (gu-IN) for complex script too, not pandoc's Arabic.
gujarati_pdf_to_word.py, the whole script
"""Gujarati PDF to Word: an editable .docx. The script of brasspage.com/gujarati-pdf-to-word/
Needs on your PATH: Poppler (pdftotext, pdftoppm), Tesseract with the guj model, and pandoc. Python 3, stdlib only.
python3 gujarati_pdf_to_word.py in.pdf out.docx [tessdata folder]
Each page: the PDF's own text (pdftotext -raw: plain pdftotext put a space inside 4 to 6 Gujarati words of 1,211 in
our test, -raw in 1; -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 Gujarati letters (a scan, or an old non-Unicode font 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 guj.traineddata if your Tesseract has no guj. Tesseract can write extra zero width
non-joiners (U+200C) at the end of a word; the script deletes those, so a search for the word finds it, and keeps
the ones inside a word. Blank lines split paragraphs. pandoc writes the .docx in Gujarati
(gu-IN), and set_gujarati() marks it Gujarati for complex script too (gu-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
GUJARATI = re.compile("[\u0a81-\u0ae3\u0af9-\u0aff]") # Gujarati letters and signs
ORPHAN = re.compile("(?<![\u0a95-\u0ab9\u0abc\u0af9])[\u0abe-\u0acd\u0ae2\u0ae3]") # a sign not after a consonant
END_ZWNJ = re.compile("\u200c(?![\u0a81-\u0aff])") # a zero width non-joiner with no Gujarati 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(GUJARATI.findall(t)) < 20: # ponytail: an English-only page goes to OCR too
return "no Gujarati 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 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", "guj", "--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 guj model
return END_ZWNJ.sub("", 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=gu-IN"], input=body.encode(), check=True)
set_gujarati(docx)
def set_gujarati(docx):
"""pandoc's styles keep Arabic as the complex-script language (w:bidi="ar-SA"). Make it Gujarati."""
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="gu-IN"')
z.writestr(info, data)
if __name__ == "__main__":
if len(sys.argv) < 3:
sys.exit("usage: python3 gujarati_pdf_to_word.py in.pdf out.docx [tessdata folder]")
pdf_to_docx(*sys.argv[1:4])
Scanned pages: add one flag
On clean typed pages, the Sauvola threshold did not help. On 30 real scans in our Gujarati OCR test, it lifted letters from 87.2% to 94.7%.
For a scan or a photo, add "-c", "thresholding_method=2"
to the script's tesseract line.
Our Actor: no install, no Gujarati yet
Our Apify Actor, PDF to Markdown OCR, turns PDFs into Markdown in one call. It reads 23 languages, not Gujarati 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 old non-Unicode Gujarati font.
- Typed PDFs only, plus the scan tip above.
How I tested
- Text: 12 passages from proofread gu.wikisource pages, one a page. 8 are public domain and 4 are CC BY-SA 4.0. I quote no passage, only single words.
- They hold 1,211 words, 206 viramas and 192 anusvaras.
- PDFs in Noto Sans Gujarati 2.106: Chromium 153 print to PDF and LibreOffice 26.8.1.1 export, both with lang gu, and reportlab 5.1.2 with shaping on.
- Noto Sans Gujarati has no Latin punctuation, so Chromium and LibreOffice drew those marks in another font.
- 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 guj and tessdata_best guj 4.1.0, pandoc 3.13.
- Words right: truth words found whole in the .docx text, 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 Gujarati when it printed the 4 .docx files made from the Chromium PDF, so I did not check how they look.
- Our Actor has no Gujarati 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 Gujarati, OFL.
- Scripts and results stay in our private repo, in research/bench/gujarati-pdf-to-word-2026-11-28/.
- We make the Apify Actor, not these tools.
Questions people ask
How do I convert a Gujarati PDF to Word for free?
Keep the PDF's own text where it is right, and OCR the other pages with Tesseract guj. The free script on this page does both, then pandoc writes the .docx: 1,210 of 1,211 words on 2 typed PDFs.
Why does LibreOffice break Gujarati text from a PDF?
Its PDF import splits lines into text boxes, 359 to 862 on our 3 PDFs, and puts vowel signs at the start of words. It kept 6.2% to 58.7% of words.
Can I convert a scanned Gujarati PDF to Word?
Yes. The script sends a page with no Gujarati text to Tesseract guj. For a photo of a page, add -c thresholding_method=2: on 30 real scans it lifted letters from 87.2% to 94.7%.