Files
diw/02_conversion_hwp_to_xml.py

166 lines
6.2 KiB
Python

# from pyhwpx import Hwp # 임포트
# hwp = Hwp() # 아래아한글 실행(프로그램이 실행되어 있는 경우, 기존 한/글 프로그램에 연결됨)
# hwp.insert_text("Hello world!\r\n") # 문자열 삽입
# hwp.create_table(5, 5, treat_as_char=True) # 5행5열의 표 삽입(글자처럼 취급)
# for i in range(25): # 표에 내용 삽입
# hwp.insert_text(i)
# hwp.TableRightCell()
# hwp.MoveDown()
# hwp.insert_picture("https://ultralytics.com/images/zidane.jpg") # 이미지 삽입
# hwp.ShapeObjAttachCaption() # 캡션 삽입
# hwp.insert_text("Zidane") # 캡션 문자열 입력
# hwp.ParagraphShapeAlignCenter() # 캡션 가운데정렬
# hwp.SelectAll() # 캡션 전체선택
# hwp.set_font(Bold=True, FaceName="돋움", Height=20, TextColor="Red") # 캡션 글자모양 변경
# hwp.Cancel() # 선택해제
# hwp.Close() # 캡션 편집 종료
import win32com.client
import os
import logging
import shutil
from pathlib import Path
from pywinauto import Application
from itertools import chain
import time
import win32com.client.gencache
def setup_logging():
"""로깅 설정"""
logging.basicConfig(
level = logging.INFO,
format = '%(asctime)s - %(levelname)s - %(message)s',
handlers = [
logging.FileHandler('hwp_conversion.log', encoding='utf-8'),
logging.StreamHandler()
]
)
def convert_hwp_to_xml(input_folder, output_folder):
"""
지정된 폴더 내의 모든 HWP 파일을 XML로 변환
Args:
input_folder (str): HWP 파일이 있는 폴더 경로
output_folder (str): XML 파일을 저장할 폴더 경로
"""
try:
# 한글 애플리케이션 객체 생성
# hwp = win32com.client.Dispatch("HWPFrame.HwpObject")
hwp = win32com.client.gencache.EnsureDispatch("HWPFrame.HwpObject")
# 자동화 보안 설정
hwp.RegisterModule("FilePathCheckDLL", "FilePathCheckerModule")
hwp.XHwpWindows.Item(0).Visible = False
# hwp.XHwpWindows.Item(0).Visible = True
# 출력 폴더가 없으면 생성
os.makedirs(output_folder, exist_ok=True)
parent_folder = os.path.dirname(output_folder)
test_folder_path = os.path.join(parent_folder, "TEST")
os.makedirs(test_folder_path, exist_ok=True)
# HWP 파일 검색 및 변환
input_path = Path(input_folder)
# for hwp_file in input_path.glob("DIW_2503B.hwpx"):
for hwp_file in chain(input_path.glob("*.hwp"), input_path.glob("*.hwpx")):
try:
# 파일 열기
if hwp_file.suffix == ".hwpx":
hwp.Open(str(hwp_file), "HWPX")
elif hwp_file.suffix == ".hwp":
hwp.Open(str(hwp_file), "HWP")
#hwpactionid 기반
hwp.Run("MoveDocBegin")
hwp.Run("SelectCtrlFront")
while True:
curr = hwp.KeyIndicator()
# print(curr[-1])
hwp.Run("SelectCtrlFront")
hwp.SetMessageBoxMode(0x111111)
hwp.Run("VtChartConverter")
next = hwp.KeyIndicator()
if next == curr:
break
hwp.SetMessageBoxMode(0x0000F0) #메시지 박스 원래대로
# XML 파일 경로 설정
xml_filename = hwp_file.stem + ".hml"
xml_path = os.path.join(output_folder, xml_filename)
# print(f"xml_path:{xml_path} type:{type(xml_path)}")
# XML로 저장
hwp.SaveAs(xml_path, "HWPML2X", "")
logging.info(f"변환 성공: {hwp_file.name} -> {xml_filename}")
except Exception as e:
logging.error(f"파일 변환 실패: {hwp_file.name} -> {e}")
finally:
# 현재 문서 닫기
hwp.Clear(3)
except Exception as e:
logging.error(f"프로그램 실행 오류: {str(e)}")
finally:
# 한글 프로그램 종료
try:
hwp.Quit()
except:
pass
def delete_gen_py():
# gen_py 디렉토리 경로
gen_py_dir = os.path.join(os.environ['LOCALAPPDATA'], 'Temp', 'gen_py')
# gen_py 디렉토리 삭제
if os.path.exists(gen_py_dir):
shutil.rmtree(gen_py_dir)
print(f'{gen_py_dir} 디렉토리를 삭제했습니다.')
if __name__ == "__main__":
exam_rounds = [
"2505",
# "2504_3"
]
delete_gen_py()
# 로깅 설정
setup_logging()
# 변환할 폴더 경로 설정
# 배열 동시에 변환하면 에러발생 -> 하나씩 변환 -> time.sleep(0.5) 추가
for exam_round in exam_rounds:
folders = [
(f"C:\\Users\\dra\\project\\HWP\\HWP-Scoring\\input\\{exam_round}\\A\\DIW",f"C:\\Users\\dra\\project\\HWP\\HWP-Scoring\\output\\{exam_round}\\A\\DIW"),
(f"C:\\Users\\dra\\project\\HWP\\HWP-Scoring\\input\\{exam_round}\\B\\DIW",f"C:\\Users\\dra\\project\\HWP\\HWP-Scoring\\output\\{exam_round}\\B\\DIW"),
(f"C:\\Users\\dra\\project\\HWP\\HWP-Scoring\\input\\{exam_round}\\C\\DIW",f"C:\\Users\\dra\\project\\HWP\\HWP-Scoring\\output\\{exam_round}\\C\\DIW"),
(f"C:\\Users\\dra\\project\\HWP\\HWP-Scoring\\input\\{exam_round}\\D\\DIW",f"C:\\Users\\dra\\project\\HWP\\HWP-Scoring\\output\\{exam_round}\\D\\DIW"),
# (f"C:\\Users\\dra\\project\\HWP\\HWP-Scoring\\input\\{exam_round}\\E\\DIW",f"C:\\Users\\dra\\project\\HWP\\HWP-Scoring\\output\\{exam_round}\\E\\DIW"),
]
# 변환 실행
for input, output in folders:
try:
logging.info(f"폴더 변환 시작: {input} -> {output}")
convert_hwp_to_xml(input, output)
logging.info(f"폴더 변환 완료: {input} -> {output}")
time.sleep(0.5)
except Exception as e:
logging.error(f"폴더 변환 중 오류 발생: {input} -> {output}, 오류: {e}")