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 — 5분 안에 HWPX 문서 다루기 — python-hwpx 7.0.3 문서 5분 안에 HWPX 문서 다루기이 가이드는
준비물pip install python-hwpx 기존 이 문서는 경로 기반 예제부터 시작한다. 손에 1. 새 문서 만들고 저장하기가장 쉬운 시작은 빈 문서를 만들고 문단 하나를 넣은 뒤 저장하는 것이다. 독립 실행 예제 — 빈 작업 디렉터리와 core wheel만으로 실행됩니다. from pathlib import Path from hwpx import HwpxDocument Path("output").mkdir(parents=True, exist_ok=True) document = HwpxDocument.new() document.add_paragraph("python-hwpx로 만든 첫 문서") document.save("output/hello.hwpx") print("저장 완료: output/hello.hwpx")
2. 기존 문서 열어 수정하기이미 있는 파일을 고칠 때는 from pathlib import Path from hwpx import HwpxDocument Path("output").mkdir(parents=True, exist_ok=True) with HwpxDocument.open("output/hello.hwpx") as document: document.add_paragraph("자동화로 추가한 검토 문단") document.save("output/hello-updated.hwpx") print("저장 완료: output/hello-updated.hwpx")
3. 문서 읽기 — 텍스트 추출고치기 전에 무엇이 들어 있는지부터 보고 싶을 때가 많다. 방금 만든 파일의 텍스트를 그대로 꺼내 보자. from hwpx import TextExtractor extractor = TextExtractor("output/hello-updated.hwpx") print(extractor.extract_text()) 서식·중첩 표·각주까지 함께 다루는 순회 방법은 사용 가이드와 API 레퍼런스에서 이어진다. 4. 첫 성공 다음에 자주 하는 편집문서를 한 번 저장해봤다면, 그다음은 보통 문단, 표, 메모 순서로 간다. 아래는 2번의 결과 파일을 이어받아 그대로 실행되는 한 덩어리 스크립트다. from hwpx import HwpxDocument document = HwpxDocument.open("output/hello-updated.hwpx") # 문단 추가와 삭제 paragraph = document.add_paragraph("잠시 있다 사라질 문단") print("추가된 문단:", paragraph.text) paragraph.remove() # 표와 메모 추가 section = document.sections[0] paragraph = document.add_paragraph("검토용 문단", section=section) table = document.add_table(rows=2, cols=2, section=section) table.set_cell_text(0, 0, "항목") table.set_cell_text(0, 1, "값") table.set_cell_text(1, 0, "상태") table.set_cell_text(1, 1, "검토 중") memo = document.notes.add_memo( "이 문단을 다시 확인하세요.", paragraph=paragraph, ) print("메모 ID:", memo.id) print("필드 ID:", memo.field_id) document.save("output/hello-final.hwpx") print("저장 완료: output/hello-final.hwpx") 참고 섹션에는 최소 하나의 단락이 필요합니다. 마지막 단락을 삭제하면 메모 모양은 메모마다가 아니라 구역마다다(한/글과 같다). 모양을 바꾸려면 5. 저장에는 영수증이 따라온다
from hwpx import HwpxDocument document = HwpxDocument.open("output/hello-final.hwpx") document.add_paragraph("영수증 확인용 문단") report = document.save("output/hello-receipt.hwpx") print("저장 모드:", report.actual_mode) print("미수정 파트 검증:", report.preservation.untouched_part_payloads.to_dict())
6. 저장 방식 더 보기기본은 경로를 주는 from io import BytesIO buf = BytesIO() document.save(buf) raw = document.to_bytes() print("바이트 길이:", len(raw)) 7. 파일이 없으면 기본 템플릿 바이트로 열기파일 경로 없이도 바로 문서를 열 수 있다. 다만 이건 첫 성공 다음 단계로 보면 된다. 독립 실행 예제 — 내장 템플릿 바이트만 사용합니다. from io import BytesIO from hwpx import HwpxDocument from hwpx.templates import blank_document_bytes source = BytesIO(blank_document_bytes()) document = HwpxDocument.open(source) print("총 섹션 수:", len(document.sections)) 다음 단계
|
| You are visitor 01402775 — 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. |