37 lines
977 B
Python
37 lines
977 B
Python
from pyhwpx import Hwp
|
|
|
|
hwp = Hwp()
|
|
hwp.FileNew()
|
|
|
|
# HTML 헤딩 레벨 → 한글 기본 스타일 매핑
|
|
heading_style_map = {
|
|
'h1': 1, # 개요 1
|
|
'h2': 2, # 개요 2
|
|
'h3': 3, # 개요 3
|
|
'h4': 4, # 개요 4
|
|
'h5': 5, # 개요 5
|
|
'h6': 6, # 개요 6
|
|
}
|
|
|
|
def apply_heading_style(text, tag):
|
|
"""HTML 태그에 맞는 스타일 적용"""
|
|
hwp.insert_text(text)
|
|
hwp.HAction.Run("MoveLineBegin")
|
|
hwp.HAction.Run("MoveSelLineEnd")
|
|
|
|
# 해당 태그의 스타일 번호로 적용
|
|
style_num = heading_style_map.get(tag, 0)
|
|
if style_num:
|
|
hwp.HAction.Run(f"StyleShortcut{style_num}")
|
|
|
|
hwp.HAction.Run("MoveLineEnd")
|
|
hwp.BreakPara()
|
|
|
|
# 테스트
|
|
apply_heading_style("1장 서론", 'h1')
|
|
apply_heading_style("1.1 연구의 배경", 'h2')
|
|
apply_heading_style("1.1.1 세부 내용", 'h3')
|
|
apply_heading_style("본문 텍스트", 'p') # 일반 텍스트
|
|
|
|
hwp.SaveAs(r"D:\test_output.hwp")
|
|
print("완료!") |