본문 바로가기
아래아한글 자동화/python+hwp 중급

[python+hwp] 파이썬+한/글로 가장 강력한 문자열 가공 프로그램 만들기 #스크립트txt를 영상자막용 srt포맷으로 변경하기

by 일코 2023. 2. 8.

아래 유튜브 영상의 소스코드를 공개합니다.

 

예시문서(txt)를 3개 첨부해 두었습니다.

동영상_txt_srt_각3개.zip
13.89MB

txt 스크립트 출처

talkpython/mastering-pycharm-course: Course demos and handouts for Talk Python's Effective PyCharm course (github.com) 

 

GitHub - talkpython/mastering-pycharm-course: Course demos and handouts for Talk Python's Effective PyCharm course

Course demos and handouts for Talk Python's Effective PyCharm course - GitHub - talkpython/mastering-pycharm-course: Course demos and handouts for Talk Python's Effective PyCharm course

github.com

영상출처

Effective PyCharm Online Course - [Talk Python Training]

 

Effective PyCharm Online Course - [Talk Python Training]

Sorry, your browser doesn't support this video format. This course is carbon neutral. Course Summary PyCharm is the premier Python IDE (integrated development environment). You will be hard pressed to find an editor that gives a more holistic way to build

training.talkpython.fm

 

아래는 코드 전문입니다.

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()

영상을 먼저 보시는 것을 추천드립니다.

행복한 하루 되세요!

 


https://inf.run/3Bo3

 

움짤로 빠르게 배우는 파이썬-아래아한글 자동화 레시피 - 인프런 | 강의

파이썬으로 아래아한글을 다루는 짧은 예제코드들을 소개하고, 중간중간의 결과를 GIF로 보여드립니다. 동영상 강의가 아니지만 오히려 빠르게 배울 수 있고, 따라하기도 쉽습니다., - 강의 소개

www.inflearn.com

 

댓글