استخراج النص الأساسي باستخدام Python

استخراج النص من جميع صفحات وثيقة PDF

استخدم ممتص النص لالتقاط كل النص من كل صفحة من وثيقة PDF وكتابته إلى ملف نصي. هذا الأسلوب مناسب تمامًا لتحويل ملفات PDF إلى نص يمكن البحث فيه أو تشغيل تحليل المحتوى أو إعداد النص للفهرسة والمعالجة النهائية.

  1. افتح مستند PDF باستخدام مستند.
  2. قم بإنشاء TextAbsorber مثال.
  3. اتصل document.pages.accept(text_absorber) لمسح جميع الصفحات.
  4. استرجع النص المستخرج من text_absorber.text.
  5. اكتب النتيجة إلى ملف نصي الإخراج.
import os
import aspose.pdf as ap


def extract_text_from_all_pages(infile, outfile):
    """
    Extract all text from every page of the PDF and write to an output text file.
    Args:
        infile (str): Path to input PDF file.
        outfile (str): Path to output text file.
    """
    # Open the PDF document
    document = ap.Document(infile)
    # Create a TextAbsorber to extract text
    text_absorber = ap.text.TextAbsorber()
    # Accept the absorber for all pages
    document.pages.accept(text_absorber)
    # Get extracted text
    extracted_text = text_absorber.text
    # Write the text to an output file
    with open(outfile, "w", encoding="utf-8") as tw:
        tw.write(extracted_text)

استخراج نص من صفحة معينة

تقدم بطلبك ممتص النص إلى صفحة واحدة لعزل النص وحفظه من هذا القسم من مستند متعدد الصفحات. يكون هذا مفيدًا عندما تحتاج إلى محتوى من صفحة واحدة فقط - على سبيل المثال، فاتورة أو قسم تقرير أو ملخص نموذج.

  1. افتح مستند PDF باستخدام مستند.
  2. قم بإنشاء TextAbsorber مثال.
  3. اتصل accept على الصفحة المستهدفة: document.pages[page_number].accept(text_absorber).
  4. استرجع النص المستخرج واكتبه في ملف.
import os
import aspose.pdf as ap


def extract_text_from_page(infile, outfile, page_number):
    """
    Extract text from a specific page number of the PDF.
    Args:
        infile (str): Path to input PDF file.
        outfile (str): Path to output text file.
        page_number (int): 1-based page index to extract.
    """
    document = ap.Document(infile)
    text_absorber = ap.text.TextAbsorber()
    # Accept the absorber on only the specified page
    document.pages[page_number].accept(text_absorber)
    extracted_text = text_absorber.text
    with open(outfile, "w", encoding="utf-8") as tw:
        tw.write(extracted_text)