Loading...
Loading...
한글(HWP/HWPX) 문서를 다양한 포맷(Text, HTML, ODT, PDF)으로 변환하고, Markdown/HTML을 HWPX로 생성하는 작업을 도와줍니다. LLM/RAG 파이프라인을 위한 문서 처리, 청킹, LangChain 연동을 지원합니다.
npx skill4agent add harifatherkr/hwp-parser hwp-parsersource venv/bin/activate # 또는 .venv/bin/activatepip install -e . # 개발 모드
# 또는
pip install hwpparser[all] # 전체 기능# PDF 변환 (Chrome headless 사용)
# macOS - Chrome이 이미 설치되어 있으면 별도 설치 불필요
brew install --cask google-chrome
# Ubuntu/Debian
sudo apt install google-chrome-stable
# 또는 Chromium
sudo apt install chromium-browser
# HWPX 생성 (선택사항)
brew install pandoc # macOS
sudo apt install pandoc # Ubuntu| 키워드 | 기능 | 참조 |
|---|---|---|
| 텍스트 추출, 읽기, 파싱 | HWP → Text | |
| HTML 변환, 웹페이지 | HWP → HTML | |
| ODT, OpenDocument | HWP → ODT | |
| PDF 변환 | HWP → PDF | |
| 마크다운, 한글 생성 | Markdown → HWPX | |
| 청킹, RAG, 벡터 DB | 문서 청킹 | |
| LangChain, 문서 로더 | LangChain 연동 | |
| 일괄 변환, 폴더 처리 | 배치 변환 | |
| 검색 인덱싱, JSONL | 인덱스 생성 | |
| 메타데이터, 정보 추출 | 문서 메타데이터 | |
import hwpparser
# HWP 읽기
doc = hwpparser.read_hwp("document.hwp")
print(doc.text) # 텍스트
print(doc.html) # HTML
# 파일로 저장
doc.to_odt("output.odt")
doc.to_pdf("output.pdf")
# 빠른 변환
text = hwpparser.hwp_to_text("document.hwp")# 텍스트 추출
hwpparser text document.hwp
# 포맷 변환
hwpparser convert document.hwp output.txt
hwpparser convert document.hwp output.pdf
# 일괄 변환
hwpparser batch ./hwp_files/ -f text -o ./text_files/
# 지원 포맷 확인
hwpparser formats# 청킹 (벡터 DB용)
chunks = hwpparser.hwp_to_chunks("document.hwp", chunk_size=1000)
for chunk in chunks:
embedding = embed(chunk.text)
vector_db.insert(embedding, chunk.metadata)
# LangChain 연동
from hwpparser import HWPLoader, DirectoryHWPLoader
loader = HWPLoader("document.hwp")
docs = loader.load()
# 폴더 전체
loader = DirectoryHWPLoader("./documents", recursive=True)
docs = loader.load()
# 검색 인덱싱 (Elasticsearch/Algolia)
hwpparser.export_to_jsonl("./documents", "./index.jsonl", chunk_size=1000)# 폴더 내 모든 HWP → TXT
result = hwpparser.batch_convert("./hwp_files", "./text_files", "txt")
print(f"변환 완료: {result.success}/{result.total}")
# 모든 텍스트 합치기
all_text = hwpparser.batch_extract_text("./documents")# Markdown → HWPX
hwpparser.markdown_to_hwpx("# 제목\n내용", "output.hwpx")
# HTML → HWPX
hwpparser.html_to_hwpx("<h1>제목</h1><p>내용</p>", "output.hwpx")
# 통합 변환 인터페이스
hwpparser.convert("input.md", "output.hwpx")
hwpparser.convert("input.docx", "output.hwpx")| 입력 → 출력 | 함수/CLI |
|---|---|
| HWP → Text | |
| HWP → HTML | |
| HWP → ODT | |
| HWP → PDF | |
| Markdown → HWPX | |
| HTML → HWPX | |
| DOCX → HWPX | |
text = hwpparser.hwp_to_text("document.hwp")
print(text)result = hwpparser.batch_convert("./documents", "./pdf_output", "pdf")
print(f"성공: {result.success}, 실패: {result.failed}")# 청킹
chunks = hwpparser.hwp_to_chunks("document.hwp", chunk_size=1000)
# 벡터화 및 저장
for chunk in chunks:
embedding = your_embed_function(chunk.text)
vector_db.insert({
'embedding': embedding,
'text': chunk.text,
'metadata': chunk.metadata # file, page, offset 등
})from hwpparser import DirectoryHWPLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
# 문서 로드
loader = DirectoryHWPLoader("./documents", recursive=True)
docs = loader.load()
# 청킹
splitter = RecursiveCharacterTextSplitter(chunk_size=1000)
chunks = splitter.split_documents(docs)hwpparser.export_to_jsonl(
"./documents",
"./search_index.jsonl",
chunk_size=1000 # 청킹 포함
)hwpparser.convert("README.md", "README.hwpx")meta = hwpparser.extract_metadata("document.hwp")
print(f"글자 수: {meta['char_count']}")
print(f"단어 수: {meta['word_count']}")from hwpparser.exceptions import (
HWPFileNotFoundError,
ConversionError,
DependencyError,
UnsupportedFormatError
)
try:
result = hwpparser.convert("document.hwp", "output.pdf")
except HWPFileNotFoundError:
print("파일을 찾을 수 없습니다")
except DependencyError as e:
print(f"의존성 누락: {e}")
except ConversionError as e:
print(f"변환 실패: {e}")# 전체 테스트
pytest tests/ -v
# 특정 모듈 테스트
pytest tests/test_reader.py -v
# 커버리지
pytest tests/ --cov=hwpparserpyhwp