Kako napraviti model laganog dokumenta u Pythonu?

Kako napraviti model laganog dokumenta u Pythonu?

Model svjetlosnog dokumenta (LDM) je Aspose.Words Python-native predstavljanje FOSS-a. Ovaj vodič pokazuje kako učitati DOCX datoteku i pretvoriti je u Word dokument. LDM korištenje DocumentReader i njenu to_light_document() metoda od LdmBuilderMixin.


U skladu s člankom 21. stavkom 1.

Odluka o odobrenjuDetaljnije informacije
Python3.9 or later
Sljedeći članak:aspose-words-foss (MIT-licenciran)
Uvodni broj:A. - U redu je. .docx datoteka
pip install aspose-words-foss

Korak 1 Uvoz čitača dokumenata

from aspose.words_foss import DocumentReader

DocumentReader nasljeđuje od: LdmBuilderMixin, što omogućuje: to_light_document() metoda.


Korak 2 Uplatiti DOCX datoteku

Upotreba: load_file() za putanje datoteke, load_stream() za objekt sličan datoteki, ili load_bytes() za neispravne bajtove:

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

Korak 3 Izgraditi LDM-a

Zovi me. to_light_document() za pretvaranje učvršćene DOCX-a u LDM. Document:

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

Korak 4 Pregledajte dokument

LDM-a Document U tom slučaju, u skladu s člankom 3. stavkom 1. točka (a) točke:

# 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)

Korak 5 Napisati natrag na DOCX (neopcionalno)

Nakon provjere ili modifikacije LDM-a, upotrebljavajte: LdmDocxWriter za proizvodnju DOCX datoteke:

from aspose.words_foss import LdmDocxWriter

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

Sljedeći koraci

Također pogledajte:

 Hrvatski