Build a Light Document Model

כיצד לבנות מודל מסמך אור ב- Python

כיצד לבנות מודל מסמך אור ב- Python

מודל המסמך האור (LDM) הוא Aspose.Words של FOSS Python-native representation מדריך זה מראה כיצד להעלות קובץ DOCX ולהמיר אותו לתוך מסמך LDM משתמשים DocumentReader ועם שלה to_light_document() שיטת מ LdmBuilderMixin.


דרישות מוקדמות

דרישותפרטים
Python3.9 or later
חבילהaspose-words-foss (הוא מורשה על ידי
כניסהא .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")

שלבים הבאים

ראה גם

 עברית