Код: Выделить всё
import pdfplumber
from bidi.algorithm import get_display
import arabic_reshaper
import re
def clean_text(text):
# Remove NULL bytes and control characters
cleaned_text = re.sub(r'[\x00-\x1F\x7F]', '', text)
return cleaned_text
def reshape_and_bidi_text(text):
# Reshape Arabic text and apply bidi algorithm
reshaped_text = arabic_reshaper.reshape(text)
bidi_text = get_display(reshaped_text)
return bidi_text
def extract_text_from_pdf(pdf_path):
text = ""
with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
page_text = page.extract_text()
if page_text:
text += page_text + "\n"
return text
def save_text_to_file(text, output_path):
with open(output_path, "w", encoding="utf-8") as text_file:
text_file.write(text)
def convert_pdf_to_text(pdf_path, output_path):
# Extract text from the PDF using pdfplumber
extracted_text = extract_text_from_pdf(pdf_path)
# Clean the extracted text
cleaned_text = clean_text(extracted_text)
# Reshape and apply bidi algorithm to the text
reshaped_bidi_text = reshape_and_bidi_text(cleaned_text)
# Save the cleaned and reshaped text to a text file
save_text_to_file(reshaped_bidi_text, output_path)
print(f"Text from {pdf_path} has been saved to {output_path}")
# Example usage
pdf_path = r'C:\Users\DELL\Desktop\Book Printed\البوليميرات العالية الأداء.pdf'
text_output_path = r"C:\Users\DELL\Desktop\output.txt"
convert_pdf_to_text(pdf_path, text_output_path)
Примечание : прикреплено выше https://www.noor-book.com/%D9%83%D8%AA% ... 8%D9%88%D9. %84%D9%8A%D9%85%D9%8A%D8%B1%D8%A7%D8%AA-%D8%A7%D9%84%D8%B9%D8%A7%D9%84%D9% 8A%D8%A9-%D8%A7%D9%84%D8%A3%D8%AF%D8%A7%D8%A1-pdf?next=72c6f38a363b368a7bd978a8449ea530 — книга на арабском языке, которую я пытаюсь читать
Подробнее здесь: https://stackoverflow.com/questions/786 ... ing-python