当前位置:   article > 正文

将txt或者world转为rst字幕_world转成srt文件

world转成srt文件
import datetime,docx

def srt_format(timedelta, line_number=1,     start_time=datetime.datetime.strptime('00:00:00,0', '%H:%M:%S,%f'),
               font_clor="#B87333", font_size=20, font_name="宋体"):
    """
    :param timedelta:      时间间隔,单位 毫秒
    :param line_number:    字幕序列号
    :param start_time:      字幕起始时间
    :param font_clor:       字幕字体颜色
    :param font_size:       字幕字体大小
    :param font_name:        字幕字体类别
    :return:                字幕格式
    """
    line = ''
    line += str(line_number) + '\n'
    end_time = start_time + datetime.timedelta(milliseconds=timedelta)
    str_start_time = datetime.datetime.strftime(start_time, '%H:%M:%S,%f')
    str_end_time = datetime.datetime.strftime(end_time, '%H:%M:%S,%f')[:-3]
    line += str_start_time + " --> " + str_end_time + "\n"
    line += "<font=" + font_clor + ">"
    line += "{\\fn" + font_name + "\\fs" + str(font_size) + "}"
    return line



def txt2rst(read_path='read.txt',write_path='write.rst',time_path=''):
    with open(read_path,'r',encoding='UTF-8') as read_txt,\
            open(write_path,'w',encoding='UTF-8-sig') as write_file:
        line = read_txt.readline()
        n_line = 1
        while line:
            write_line = srt_format(3600,n_line)+line+'\n'
            write_file.write(write_line)
            line = read_txt.readline()
            n_line += 1


def docx_to_rst(read_path='text.docx',write_path='write.rst',time_path=''):
    # 打开文件
    file = docx.opendocx(read_path)
    # 读取文本内容
    text = docx.getdocumenttext(file)
    # 打印输出到屏幕
    with open(write_path, 'w', encoding='UTF-8-sig') as write_file:
        for n_line,line in enumerate(text,1):
            write_line = srt_format(3600, n_line) + line + '\n' +'\n'
            write_file.write(write_line)
    return
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/379720
推荐阅读
相关标签
  

闽ICP备14008679号