Files
diw/00_dividepage.py

67 lines
2.4 KiB
Python

import win32com.client as win32
import os
import time
constants = win32.constants
hwp = None
try:
hwp = win32.gencache.EnsureDispatch("HWPFrame.HwpObject")
hwp.RegisterModule("FilePathCheckDLL", "SecurityModule")
hwp.XHwpWindows.Item(0).Visible = False
file_path = "D:\\SynologyDrive\\develop\\py\\hwp\\sample\\sample.hwpx"
output_file_path = "D:\\SynologyDrive\\develop\\py\\hwp\\sample\\sample.xml"
if not os.path.exists(file_path):
print(f"❌ 오류: 원본 파일 '{file_path}'이 존재하지 않습니다.")
exit()
print(f"'{file_path}' 파일을 엽니다...")
hwp.Open(file_path)
total_pages = hwp.PageCount
current = 1
hwp.HAction.Run("MoveDocBegin")
while( current <= total_pages):
# 북마크 삽입 (현재 커서 위치에 "Page_쪽_start","Page_쪽_end" 이름으로)
hwp.HAction.GetDefault("Bookmark", hwp.HParameterSet.HBookMark.HSet)
hwp.HParameterSet.HBookMark.name = "Page_" + str(current) + "_start"
hwp.HParameterSet.HBookMark.type = 0
hwp.HParameterSet.HBookMark.Command = 0
hwp.HAction.Execute("Bookmark", hwp.HParameterSet.HBookMark.HSet)
if current < total_pages:
hwp.HAction.Run("MovePageDown")
hwp.HAction.Run("MoveLeft")
else:
hwp.HAction.Run("MoveDocEnd")
hwp.HAction.GetDefault("Bookmark", hwp.HParameterSet.HBookMark.HSet)
hwp.HParameterSet.HBookMark.name = "Page_" + str(current) + "_end"
hwp.HParameterSet.HBookMark.type = 0
hwp.HParameterSet.HBookMark.Command = 0
hwp.HAction.Execute("Bookmark", hwp.HParameterSet.HBookMark.HSet)
hwp.HAction.Run("MoveRight")
current += 1
# --- 저장 ---
output_dir = os.path.dirname(output_file_path)
if output_dir and not os.path.exists(output_dir):
os.makedirs(output_dir)
hwp.SaveAs(output_file_path, "HWPML2X", "")
print(f"'{output_file_path}'에 저장 완료.")
except Exception as e:
print(f"❌ 오류 발생: {e}")
print("여전히 문제가 발생합니다. 한글 프로그램 재설치 또는 환경 문제일 가능성이 큽니다.")
finally:
if hwp:
try:
hwp.Quit(saveoption=0)
print("✅ 한글 프로그램 종료.")
except Exception as e:
print(f"⚠️ 한글 프로그램 종료 중 오류 발생: {e}")
print("✅ 최종 작업 시도 완료!")