كيفية بناء نموذج وثيقة خفيفة في بايثون

كيفية بناء نموذج وثيقة خفيفة في بايثون

نموذج الوثيقة الخفيفة (LDM) هو Aspose.Words تمثيل Python الأصلي لـ FOSS من مستند Word. هذا الدليل يوضح كيفية تحميل ملف DOCX وتحويله إلى LDM باستخدام DocumentReader و … to_light_document() طريقة من LdmBuilderMixin.


المعايير

متطلباتالتفاصيل
Python3.9 or later
حزمةaspose-words-foss (مصدر من MIT)
المدخلات(أ) .docx الملف
pip install aspose-words-foss

الخطوة 1 استيراد DocumentReader

from aspose.words_foss import DocumentReader

DocumentReader يرث من LdmBuilderMixin,، والتي توفر الميزانية to_light_document() طريقة.


الخطوة 2 تحميل ملف DOCX

استخدام load_file() لطرق الملف،, load_stream() لأجسام تشبه الملفات، أو load_bytes() بالنسبة للبايت الخام:

reader = DocumentReader()
reader.load_file("data/my_document.docx")

الخطوة 3 بناء LDM

اتصلوا to_light_document() لتحويل DOCX المحمول إلى LDM. Document:

doc = reader.to_light_document()
print("Page count:", doc.page_count)
print("Sections:", len(doc.sections))
print("Paragraphs:", len(doc.all_paragraphs))

الخطوة 4 فحص المستند

الـ LDM Document يظهر شجرة المستند الكاملة. نقاط الدخول الشائعة:

# Read full text
print(doc.text[:300])

# Iterate sections
for section in doc.sections:
    print("Section paragraphs:", len(section.paragraphs))

# Find headings (up to H2)
for heading in doc.headings(max_level=2):
    print("Heading:", heading.text)

الخطوة 5 إعادة الكتابة إلى DOCX (اختياري)

بعد فحص أو تعديل LDM، استخدم: LdmDocxWriter لإنتاج ملف DOCX:

from aspose.words_foss import LdmDocxWriter

writer = LdmDocxWriter()
writer.write(doc, "output/result.docx")

الخطوات التالية

انظر أيضا

 العربية