Skip to main content

Arabic PDF text extraction with ligature and RTL fixes

Project description

PDF2Text-Arabic

Arabic PDF text extraction built on PyMuPDF. Fixes ligature decomposition, RTL ordering, table extraction, and other issues that make raw PyMuPDF output unusable for Arabic.

What it fixes

# Problem Fix
1 Ligature decomposition — PyMuPDF breaks Arabic ligatures (الله, لأ, لإ) into LTR-ordered zero-width chars Detects zero-width clusters, reverses to RTL order
1b Lam-Alef swap — لا ligature decomposed as ال (alef before lam) Detects width ratio, swaps to correct order
2 Presentation Forms — Returns U+FB50–FDFF / U+FE70–FEFF instead of standard Arabic NFKC normalization
3 Line splitting — One visual line split into multiple rawdict lines at same y Y-coordinate merging with tolerance
4 Number reversal — RTL sorting reverses digit sequences (2019 → 9102) Detects LTR digit runs, reverses back
5 Arabic↔digit spacing — No space between Arabic text and numbers Regex-inserts spaces at boundaries
6 Artifact spaces — Space chars with overlapping bboxes cause false word breaks Only honors spaces with physical gaps > 0.5px
7 Invisible chars — Zero-width joiners, BOM, LTR/RTL marks, kashida Stripped in post-processing

Install

pip install pdf2text-arabic

From source:

pip install .
# or with uv
uv pip install .

Quick start

Python API

from pdf2text_arabic import extract_pdf, extract_page

# Extract entire PDF
text = extract_pdf("document.pdf")

# With cropping (remove headers/page numbers)
text = extract_pdf("document.pdf", crop_top=50, crop_bottom=30)

# Crop by percentage
text = extract_pdf("document.pdf", crop_top=5, crop_bottom=3, crop_unit="pct")

# Disable footnote separator detection
text = extract_pdf("document.pdf", detect_footer=False)

Single page

import fitz
from pdf2text_arabic import extract_page

doc = fitz.open("document.pdf")
text = extract_page(doc[0], crop_top=50, crop_bottom=30)
doc.close()

CLI

# Process all PDFs in a directory
pdf2text-arabic -i ./download -o ./output/plain_text

# Single file
pdf2text-arabic -f document.pdf -o ./output

# With cropping
pdf2text-arabic -i ./download --crop-top 50 --crop-bottom 30

# Crop by percentage, no footer detection
pdf2text-arabic -i ./download --crop-top 5 --crop-bottom 3 --crop-unit pct --no-footer

API reference

extract_pdf(pdf_path, **kwargs) → str

Extract text from all pages of a PDF.

Parameter Type Default Description
pdf_path str Path to the PDF file
crop_top float 0 Crop from top of each page
crop_bottom float 0 Crop from bottom of each page
crop_unit "px" | "pct" "px" Unit: points or percentage of page height
detect_footer bool True Auto-detect footnote separator lines and exclude content below

extract_page(page, **kwargs) → str

Extract text from a single fitz.Page. Same parameters as extract_pdf (except pdf_path).

Features

Table extraction

Tables are automatically detected via PyMuPDF's find_tables(), extracted with proper Arabic cell ordering, and formatted as pipe-delimited text. Merged cells are filled down so every row is self-contained:

الجهات | عدد المقاعد | مقر الدائرة الانتخابية
طنجة – تطوان – الحسيمة | 2 | ولاية جهة فاس - مكناس
الشرق | 2 | ولاية جهة فاس - مكناس
فاس - مكناس | 2 | ولاية جهة فاس - مكناس

Footer detection

Automatically detects horizontal separator lines (both vector drawings and text-based dashes) in the bottom 40% of each page and excludes footnote text below them. Handles non-selectable drawn lines and selectable ------ text.

Page cropping

Crop headers and page numbers by fixed pixel amount or percentage of page height.

Project structure

pdf2text_arabic/
├── __init__.py    # Public API: extract_pdf, extract_page
├── _chars.py      # Character-level ligature/overlap fixes
├── _text.py       # RTL text building, cleaning, line merging
├── _tables.py     # Table detection and formatting
├── _footer.py     # Footer separator detection
├── _extract.py    # Page/PDF extraction orchestration
└── cli.py         # CLI entry point

Integration with other projects

pip install pdf2text-arabic
from pdf2text_arabic import extract_pdf

def extract_law_text(path: str) -> str:
    return extract_pdf(path, crop_top=50, crop_bottom=30, detect_footer=True)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pdf2text_arabic-0.1.1.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pdf2text_arabic-0.1.1-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file pdf2text_arabic-0.1.1.tar.gz.

File metadata

  • Download URL: pdf2text_arabic-0.1.1.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pdf2text_arabic-0.1.1.tar.gz
Algorithm Hash digest
SHA256 33c9da9c067476d6ff677d45a588ce10fd6c81bf1f3c55074f96ea777611d809
MD5 d1e7208c701e9f7f69054823024727f6
BLAKE2b-256 0c62bc7de0daaa3798594128275b281c2247bd83945377b3dbf0eeca9780d0a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pdf2text_arabic-0.1.1.tar.gz:

Publisher: publish.yml on crazytajdine/PDF2Text-Arabic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pdf2text_arabic-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pdf2text_arabic-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a27ade5cb938267bef6a9c2282c1501b2719d9548881278fe0692954913eec4a
MD5 7648cec0110c83661b21d7c5cd548a65
BLAKE2b-256 6828db37679fbbf6653a67237028af487f41e34911f655b576cfb762ad1de0fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for pdf2text_arabic-0.1.1-py3-none-any.whl:

Publisher: publish.yml on crazytajdine/PDF2Text-Arabic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page