반응형
아래 유튜브 영상의 소스코드를 공개합니다.
예시문서(txt)를 3개 첨부해 두었습니다.
txt 스크립트 출처
영상출처
Effective PyCharm Online Course - [Talk Python Training]
아래는 코드 전문입니다.
import datetime as dt
import os
import tkinter as tk
from tkinter.filedialog import askopenfilenames
import win32com.client as win32
def init_hwp(visible=False):
hwp = win32.gencache.EnsureDispatch("hwpframe.hwpobject")
hwp.XHwpWindows.Item(0).Visible = visible
hwp.RegisterModule("FilePathCheckDLL", "FilePathCheckerModule")
return hwp
def get_text():
hwp.InitScan(Range=0xff)
_, text = hwp.GetText()
hwp.ReleaseScan()
return text
def put_text(text):
act = hwp.CreateAction("InsertText")
pset = act.CreateSet()
pset.SetItem("Text", text)
act.Execute(pset)
# 1. 타임테이블에 시각 "00:" 추가
def add_hour(length):
hwp.Run("MoveDocBegin")
for _ in range(length):
put_text("00:")
hwp.Run("MoveNextParaBegin")
hwp.Run("MoveDocBegin")
# 2. 타임테이블 끝에 ",000" 추가, 엔터, 다음줄 엔터
def add_microsec(length):
for _ in range(length):
hwp.HAction.Run("MoveSelParaEnd")
hwp.InitScan(Range=0xff)
_, text = hwp.GetText()
hwp.ReleaseScan()
text = text.split("\xa0", maxsplit=1)
text[0] = text[0] + ",000 \r\n" # 00:00:01,000
text[1] = text[1] + "\r\n\r\n"
text = "".join(text)
put_text(text)
hwp.HAction.Run("MoveNextParaBegin")
hwp.Run("MoveDocBegin")
# 3. 화살표( --> ) 추가
def add_arrow(length):
"""
화살표를 추가하는 함수
"""
for _ in range(length):
hwp.Run("MoveParaEnd")
put_text(" --> ")
for _ in range(4):
hwp.Run("MoveNextParaBegin")
hwp.Run("MoveDocBegin")
# 4. 다음타임스탬프 가져다 붙이기
def add_next_timestamp(length):
for _ in range(length - 1):
for _ in range(4):
hwp.Run("MoveNextParaBegin")
hwp.Run("MoveSelNextWord")
text = get_text()
for _ in range(4):
hwp.Run("MovePrevParaEnd")
put_text(text.strip())
for _ in range(4):
hwp.Run("MoveNextParaBegin")
last_time = dt.datetime.strptime(text, "%H:%M:%S,000 ") + dt.timedelta(seconds=5)
last_time = last_time.strftime("%H:%M:%S,000")
hwp.Run("MoveParaEnd")
put_text(last_time)
hwp.Run("MoveDocBegin")
hwp.Run("BreakPara")
hwp.Run("MoveUp")
# 5. 마지막으로 인덱스 붙이기
def add_index(length):
for i in range(length):
put_text(str(i + 1))
for _ in range(4):
hwp.Run("MoveNextParaBegin")
def get_filelist():
win = tk.Tk()
win.withdraw()
filelist = askopenfilenames(title="서식을 변경할 문서를 모두 선택해주세요.",
initialdir=os.getcwd(), filetypes=[("txt문서", "*.txt")])
return filelist
def remove_last_blank_lines():
hwp.Run("MoveDocEnd")
while True:
hwp.Run("MoveSelParaBegin")
text = get_text()
if not text:
hwp.Run("DeleteBack")
else:
hwp.Run("MoveDocBegin")
break
def message_box():
msgbox = hwp.XHwpMessageBox # 메시지박스 생성
msgbox.string = f"변환작업이 완료되었습니다."
msgbox.Flag = 0 # [확인] 버튼만 나타나게 설정
msgbox.DoModal() # 메시지박스 보이기
if __name__ == '__main__':
hwp = init_hwp()
filelist = get_filelist()
for file in filelist:
hwp.Open(file)
hwp.XHwpWindows.Item(0).Visible = True
remove_last_blank_lines()
length = hwp.XHwpDocuments.Item(0).XHwpSummaryInfo.Paragraphs
path = hwp.Path
add_hour(length)
add_microsec(length)
add_arrow(length)
add_next_timestamp(length)
add_index(length)
hwp.SaveAs(path.replace(".txt", ".srt"), Format="TEXT")
hwp.XHwpWindows.Item(0).Visible = False
message_box()
get_filelist()
hwp.Clear(1)
hwp.Quit()
영상을 먼저 보시는 것을 추천드립니다.
행복한 하루 되세요!
반응형
'아래아한글 자동화 > python+hwp 중급' 카테고리의 다른 글
[python+hwp] 불규칙한 데이터의 삽입 feat. [문서끼워넣기, 데이터프레임] (0) | 2023.02.09 |
---|---|
[python+hwp] 생각보다 hwp 업무자동화가 어렵다면... (1) | 2023.02.01 |
[QnA] 글상자 내용을 밖으로 꺼내기 (0) | 2023.01.27 |
댓글