티스토리 뷰

 

def ConvertSlidenotes2Subtitles(pptxFile, outFile):
    # 파워포인트 프레젠테이션을 엽니다.
    if not os.path.exists(pptxFile):
        print("[???] None of PPTX file of ", pptxFile)
        return;
    presentation = pptx.Presentation(pptxFile)

    # 슬라이드 노트의 텍스트를 수집합니다.
    subtitles = []
    for slide1 in presentation.slides:
        slideNote1 = slide1.notes_slide
        if not slideNote1: continue;
        textFrame1 = slideNote1.notes_text_frame 
        if not textFrame1:
            subtitles.append("")
            continue;
        subtitles.append(textFrame1.text)
        
    if not subtitles:
        print("[??] None of Subtitles");
        return;

    # 자막을 SRT 파일로 저장합니다.
    with open(output_file, 'w') as f:
        timeCount = 0
        subtitleCount = 0
#        f.write("# 타임코드\t자막\n")
        for subTitle1 in subtitles:
            if not subTitle1: timeCount += 1; continue;
            lines1 = subTitle1.split("\n")
            subTitle11 = lines1[0]
            subtitleCount += 1
            f.write(str(subtitleCount) + "\n")
            startSecond1 = timeCount*5
            endSecond1 = startSecond1 + 5
            startMiinute11 = startSecond1//60
            startSecond11 = startSecond1%60
            endMinute11 = endSecond1//60
            endSecond11 = endSecond1%60
            startString1 = "00:" + str(startMiinute11).zfill(2) + ":" + str(startSecond11).zfill(2) + ",000"
            endString1 = "00:" + str(endMinute11).zfill(2) + ":" +str(endSecond11).zfill(2) + ",000"
            timeString1 = startString1 + " --> " + endString1
            f.write(f'{timeString1}\n')
            f.write(f'{subTitle11}\n')
            f.write("\n")

            timeCount += 1

    print("Completed")

def Test01(fileTitle=None):
    if fileTitle:
        presentationFile = "E:/Temp/ASB/" + fileTitle + ".pptx"
        outFile = "E:/Temp/ASB/" + fileTitle + ".srt"
    else: 
        presentationFile = 'E:/Temp/ASB/ASB_IT.pptx'
        outFile = 'subtitles.srt'

    # 자막 파일 경로를 지정합니다.

    # 자막을 변환합니다.
    ConvertSlidenotes2Subtitles(presentationFile, outFile)
반응형