개요
이걸 왜 만들었냐면 2021년에 정보처리기사 공부하다가 파일이 전부 hwp 확장자였다.
hwp나 pptx는 어플에서 불러오지 못하거나 깨지는 경우가 많아서 전부 pdf로 바꿔주어야 했는데
2011년부터 2021년까지 10년치 기출문제를 전부........바꿔주기엔 너무 귀찮았다.
(무엇보다 동기부여가 안됐다)
한글과 컴퓨터 API는 링크를 참고
(좀 불친절하다)
이 역시 동영상 프레임 추출 프로그램처럼 GUI에는 tkinter 라이브러리를 사용했고,
exe 파일로 만드는데에는 pyinstaller 라이브러리를 사용하였다.
사용 라이브러리
pywin32 : 한글 프로그램을 다루기 위해 설치해야하는 라이브러리
코드
from glob import glob
import win32com.client as win32
def hwpToPdf(file, path):
if file == '' or path == '':
print("[에러] 경로가 지정되지 않았습니다.")
return
else:
print("-------------------------필독!-------------------------\n")
print("\"한글을 이용하여 위 파일에 접근하려는 시도가 있습니다.\"")
print("와 같은 팝업창이 뜰 경우 [모두 허용]을 눌러주세요.\n")
print("-------------------------------------------------------")
file = glob(file + '\*.hwp')
hwp=win32.gencache.EnsureDispatch("HWPFrame.HwpObject")
print("프로그램 강제 종료를 원할 경우 cmd 창을 닫아주세요.\n")
print("-------------------------------------------------------")
for i in file:
hwp.Open(i)
i = i.split('\\')
i.reverse()
hwp.SaveAs(path + '/' + i[0].replace('.hwp', '.pdf'), "PDF")
print("변환 완료 :", path + '/' + i[0])
hwp.Quit()
print("-------------------------변환 완료!-------------------------\n")
return
핵심인 hwptopdf 코드이다.
file = glob(file + '\*.hwp')
hwp = win32.gencache.EnsureDispatch("HWPFrame.HwpObject")
glob를 통해 폴더 내의 모든 hwp파일(의 경로)를 가져온다.
그 후 win32 라이브러리를 통해 hwp 변수를 선언하여 한글 프로그램에 접근한다.
for i in file:
hwp.Open(i)
...
hwp.SaveAs(path + '/' + i[0].replace('.hwp', '.pdf'), "PDF")
...
hwp.Quit()
file에 저장된 hwp 파일의 경로를 하나씩 불러와 한글 프로그램에서 open 한다.
그 후 hwp.SaveAS 함수를 사용하여 저장형식을 "PDF"로 선언하고 확장자를 pdf로 바꿔 파일을 저장한다.
from glob import glob
import win32com.client as win32
import tkinter as tk
import tkinter.filedialog as fd
from os import path
init_dir = path.join(path.expanduser('~'),'Desktop')
def pathload(arrow, textfield):
global init_dir
init_dir = fd.askdirectory(initialdir=init_dir, title="Select %s folder" % arrow)
textfield.delete(0, 'end')
textfield.insert(0, init_dir)
def hwpToPdf(file, path):
if file == '' or path == '':
print("[에러] 경로가 지정되지 않았습니다.")
return
else:
print("-------------------------필독!-------------------------\n")
print("\"한글을 이용하여 위 파일에 접근하려는 시도가 있습니다.\"")
print("와 같은 팝업창이 뜰 경우 [모두 허용]을 눌러주세요.\n")
print("-------------------------------------------------------")
file = glob(file + '\*.hwp')
hwp=win32.gencache.EnsureDispatch("HWPFrame.HwpObject")
print("프로그램 강제 종료를 원할 경우 cmd 창을 닫아주세요.\n")
print("-------------------------------------------------------")
for i in file:
hwp.Open(i)
i = i.split('\\')
i.reverse()
hwp.SaveAs(path + '/' + i[0].replace('.hwp', '.pdf'), "PDF")
print("변환 완료 :", path + '/' + i[0])
hwp.Quit()
print("-------------------------변환 완료!-------------------------\n")
return
root = tk.Tk()
root.title("HWPDF")
root.geometry("250x220")
root.resizable(False, False)
hwpfolder = tk.Label(root, text="HWP 파일 폴더")
hwpfolder.place(x=30, y=30)
filedirtext = tk.Entry(root)
filedirtext.place(x=30, y=50, width=135, height=25)
filebtn = tk.Button(root, text="Load", command=lambda: pathload("hwp file", filedirtext))
filebtn.place(x=170, y=50, width=50)
savefolder = tk.Label(root, text="저장 폴더")
savefolder.place(x=30, y=90)
pathdirtext = tk.Entry(root)
pathdirtext.place(x=30, y=110, width=135, height=25)
pathbtn = tk.Button(root, text="Load", command=lambda: pathload("save", pathdirtext))
pathbtn.place(x=170, y=110, width=50)
runbtn = tk.Button(root, text="Run!", height=3, command=lambda: hwpToPdf(filedirtext.get(), pathdirtext.get()))
runbtn.pack(side="bottom", fill="x")
root.mainloop()
참고
python hwp to pdf : https://chiqueen.dev/48
Python hwp to pdf
오늘 필기 공부를 위해 기출문제를 다운 받고 인쇄 하려고 봤더니 대략 100개 정도의 한글파일이 나왔다. 인쇄하기 편하기 위해 pdf로 바꾸려고 파일 하나하나 몇번씩 클릭해서 pdf로 바꾸려니 개
chiqueen.dev
한글과 컴퓨터 API : https://www.hancom.com/board/devmanualList.do
글로벌 소프트웨어의 리더, 한글과컴퓨터
www.hancom.com
'토이프로젝트 > 기타' 카테고리의 다른 글
[python-opencv] 동영상 프레임 추출 프로그램 (0) | 2022.02.21 |
---|---|
[React] Numpad 만들기 (0) | 2022.02.21 |