pdfplumber
pdfplumber copied to clipboard
Wrong Ordering of RTL Text And Table Extractions
Describe the bug
When Extracting text and tables from RTL pdf file (arabic file) , the text is being reversed and out of order, also some extra spaces are being added, the produced output is not usable at all.
Code to reproduce the problem
import pdfplumber
with pdfplumber.open("./sample.pdf") as pdf:
text = pdf.pages[0].extract_text()
print(text)
PDF file
Expected behavior
Text is extracted correctly and in order
Actual behavior
Text is reversed and has spaces added randomly (text is not usable at all)
Screenshots

Environment
- pdfplumber = "^0.7.6"
- Python 3.9.6
- Macos
Hi @ramzitannous, thanks for sharing this example. Setting layout=True, horizontal_ltr=False
should help somewhat for RTL text, but there do still appear to be bugs in the handling. I'd like to find a way to improve that.
PDFs that combine both RTL and LTR scripts are even trickier. I don't have a great solution for handling them, since I'm not aware of a foolproof way of distinguishing between the two. This seems to get partway there, although not a reliable general solution:
import pdfplumber
pdf = pdfplumber.open("./sample-ce-notice-arabic.pdf")
page = pdf.pages[0]
arabic = page.filter(lambda obj: ord(obj.get("text", "")[0]) > 200)
Output:
—
يميزعلىأساسالعرقأو الحقوقالمدنيةالفدراليةالمعمولبهاولا بقوانين يلتزم
أويعاملهم الأشخاص لايستبعد الجنس نوع السنأوالإعاقةأو أو الوطني اللونأوالأصل
نوعالجنس أوالأصلالوطنيأوالسنأوالإعاقةأو أواللون مختلفبسببالنوع علىنحو
فعالةمعنا،مثل منذويالإعاقاتللتواصلبصورة للأشخاص وخدماتمجانية مساعدات يوفر •
مترجميلغةإشارةمؤهلين ○
كبيرة،موادصوتية،تنسيقاتإلكترونيةمتيسرة، مطبوعةبأحرف معلوماتكتابيةبتنسيقاتأخرى ○
ذلكمنالتنسيقات وغير
الذينلغتهمالأساسيةليستالإنجليزية،مثل لغويةمجانيةللأشخاص يوفرخدمات •
مترجمينمؤهلين ○
معلوماتمكتوبةبلغاتأخرى ○
إذاكنتبحاجةلهذهالخدمات،اتصلبـ
فيتوفيرتلكالخدماتأوميزبطريقةأخرىعلىأساس قدأخفق إذاكنتتعتقدأن
يمكنأنتتقدمبشكوىإلى الإعاقةأوالجنس، أو السن أو أوالأصلالوطني اللون العرقأو
— ، ، ،
البريدالإلكتروني بالفاكسأو أو بالبريد بشكوىشخصيًاأو تتقدم يمكنأن ، ،
متاحلمساعدتك فإن التقدمبشكوى، إذاكنتبحاجةللمساعدةفي
وزارة بشكوىإلكترونيًالوزارة تتقدم ًضاأن يمكنأي
منخلالمكتب ، المدنية مكتبالحقوق مكتب ، الخدماتالصحيةوالبشرية
latin = page.filter(lambda obj: ord(obj.get("text", "")[0]) < 200)
print(latin.extract_text(layout=True))
Output:
Appendix A to Part 92 Sample Notice Informing Individuals About Nondiscrimination
and Accessibility Requirements and Sample Nondiscrimination Statement:
Discrimination is Against the Law
]Name of covered entity[
]Name of covered entity[ .
.
:]Name of covered entity[
:
(
)
:
]Name of Civil Rights Coordinator[
]Name of covered entity[
Name and Title of Civil [ :
TTY number TTY number if [ ]Telephone number[ ]Mailing Address[ ]Rights Coordinator
. .]Email[ ]Fax[ ]covered entity has one
. ]Name of Civil Rights Coordinator[
( Department of Health and Human Services
Office for ) ( Office for Civil Rights )
@jsvine any plans and timeline to fix this critical bug, I'm building a project around this library, and arabic extraction from pdf is a major feature.
I can't promise any particular timeline, since this is a volunteer, open-source project. But I agree that it would be great to fix, and I hope to do so when I have the time. I'm curious: What is the project?