Python에서 라이트 문서 모델 구축 방법
Python에서 라이트 문서 모델을 구축하는 방법
라이트 문서 모델(LDM)은 Aspose.Words FOSS의 Python 네이티브 Word 문서 표현입니다. 이 가이드는 DocumentReader와 LdmBuilderMixin의 to_light_document() 메서드를 사용하여 DOCX 파일을 로드하고 LDM으로 변환하는 방법을 보여줍니다.
필수 조건
| 요구 사항 | 세부 |
|---|---|
| Python | 3.9 이상 |
| Package | aspose-words-foss (MIT 라이선스) |
| Input | 하나의 .docx 파일 |
pip install aspose-words-foss1단계 — DocumentReader 가져오기
from aspose.words_foss import DocumentReaderDocumentReader은 LdmBuilderMixin을 상속하며, LdmBuilderMixin은 to_light_document() 메서드를 제공합니다.
2단계 — DOCX 파일 로드
파일 경로에는 load_file()을, 파일과 유사한 객체에는 load_stream()을, 원시 바이트에는 load_bytes()을 사용하세요:
reader = DocumentReader()
reader.load_file("data/my_document.docx")3단계 — LDM 구축
로드된 DOCX를 LDM Document으로 변환하려면 to_light_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")다음 단계
- LdmDocxWriter를 사용한 DOCX 파일 쓰기 — 전체 쓰기 API
- 문서 섹션 작업 — 섹션 및 단락 탐색