Python में एक प्रकाश दस्तावेज़ मॉडल कैसे बनाएं
Python में एक प्रकाश दस्तावेज़ मॉडल कैसे बनाएं
प्रकाश दस्तावेज़ मॉडल (LDM) Aspose.Words FOSS के Python-native प्रतिनिधित्व है यह गाइड दिखाता है कि एक DOCX फ़ाइल कैसे लोड करें और इसे एक में परिवर्तित करें LDM का उपयोग करें DocumentReader और उसकी to_light_document() तरीके से LdmBuilderMixin.
Prerequisites
| Requirement | Detail |
|---|---|
| Python | 3.9 or later |
| Package | aspose-words-foss (मैं लाइसेंस प्राप्त करता हूं) |
| Input | A .docx फ़ाइल |
pip install aspose-words-fossचरण 1 - DocumentReader का आयात करें
from aspose.words_foss import DocumentReaderDocumentReader विरासत से LdmBuilderMixin,जिससे यह प्रदान करता है कि to_light_document() तरीके से।.
चरण 2 – DOCX फ़ाइल लोड करें
उपयोग करें load_file() एक फ़ाइल मार्ग के लिए, load_stream() फ़ाइल जैसी वस्तु के लिए, या load_bytes() कच्चे बिएट के लिए:
reader = DocumentReader()
reader.load_file("data/my_document.docx")चरण 3 - LDM का निर्माण करें
Call 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 में वापस लिखें (वैकल्पिक)
एलडीएम की जांच या संशोधन के बाद, उपयोग करें LdmDocxWriter एक DOCX फ़ाइल बनाने के लिए:
from aspose.words_foss import LdmDocxWriter
writer = LdmDocxWriter()
writer.write(doc, "output/result.docx")अगले कदम
- LdmDocxWriter के साथ DOCX फ़ाइल लिखना - पूर्ण लिखित एपीआई
- दस्तावेज़ अनुभागों के साथ काम करना - अनुभाग और पैराग्राफ का पता लगाएं