赞
踩
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。