GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing airmang.github.io — 순회 레시피 — 문서에서 원하는 것 꺼내기 — python-hwpx 7.0.3 문서 순회 레시피 — 문서에서 원하는 것 꺼내기과업 중심으로 정리한 읽기·순회 레시피다. 전부 공개 API만 사용하며, ZIP이나
XML을 직접 열 필요가 없다. 예제의
문단 텍스트를 전부 읽기가장 단순한 순회다. from hwpx import HwpxDocument document = HwpxDocument.open("input.hwpx") for paragraph in document.paragraphs: print(paragraph.text) 중첩 표 안까지, 위치 정보와 함께 통독하기표 셀·각주 속 문단까지 포함해 “이 문단이 어디에 있는지”가 필요하면
from hwpx import TextExtractor extractor = TextExtractor("input.hwpx") for info in extractor.iter_document_paragraphs(include_nested=True): marker = " (중첩)" if info.is_nested else "" print(f"[{info.section.index}:{info.index}] {info.path}{marker}") print(" ", info.text()) 각주·미주를 본문과 함께 추출하기
from hwpx import TextExtractor from hwpx.tools.text_extractor import AnnotationOptions extractor = TextExtractor("input.hwpx") text = extractor.extract_text( include_nested=False, annotations=AnnotationOptions(footnote="inline", endnote="inline"), ) print(text) 각주가 있는 문단은 표를 찾아 셀 문단 읽기구조 검색은 from hwpx import ObjectFinder finder = ObjectFinder("input.hwpx") for table in finder.find_all(tag="tbl"): print("표 위치:", table.path, "| 섹션:", table.section.index) cells = finder.find_all(tag="tc") print("셀 수:", len(cells)) 런 단위로 서식과 함께 순회하기굵기·색 같은 서식은 런(run)에 붙는다. 본문 문단의 런을 순회하거나, 특정 서식의 런만 골라낼 수 있다. from hwpx import HwpxDocument document = HwpxDocument.open("input.hwpx") for run in document.text.iter_runs(): print(repr(run.text), "charPr:", run.char_pr_id_ref) red_runs = document.text.find_runs(text_color="#FF0000") print("빨간 런:", len(red_runs)) 병합 셀과 칸 좌표
메모와 누름틀(필드) 나열하기from hwpx import HwpxDocument document = HwpxDocument.open("input.hwpx") for memo in document.notes.memos: print("메모:", memo.id) for field in document.fields.all: print("누름틀:", field) 문서에 해당 요소가 없으면 빈 컬렉션이 나온다 — 예외가 아니다. 다음 단계
|
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |