import re tx0 = '\n\n\ \n\ \n' tx1 = ' \n\ \n\ \n\ \n\ \n\ \n\n\n\ \n\
\n\
\n' tx2 = '
\n \
\n' txendl = '
' def get_start_num(lines): for k, line in enumerate(lines[250:330]): hn = re.findall(r'', line[-8:]) if hn != []: snsub = re.search(r'', lines[k]) != []: return k def get_title_and_toc(lines, sn, snsub): newlines = lines[sn:] newlines[0] = lines[sn][snsub:] hnum = [] index = [] for k, line in enumerate(newlines): hn = re.findall(r'', line[-8:]) if hn != []: hn = int(hn[0]) hnum += [hn] index += [k] a0 = '' a2 = '' tmp = [] title = [] t = 0 for ind in index: line = newlines[ind] hid = re.findall(r'id="(.*?)"', line) htt = re.findall(r'">(.*?)(.*?)', '\n', '\n'] hnum += [0] toc = '' for i in range(len(hnum)-1): t1 = hnum[i] - hnum[i-1] if t1 <= 0: t1 = 0 t2 = hnum[i] - hnum[i+1] toc += lefts[t1] + tmp[i] + rights[t2] if title == []: title = re.findall(r'(.*?)\n' or len(lines)==1: print('has already modified') return old = 'class="mord mathnormal' new = 'class="mord mathdefault' sn, snsub = get_start_num(lines) en = get_end_num(lines) title, toc = get_title_and_toc(lines, sn, snsub) with open(filename,'w', encoding='UTF-8') as f: f.writelines(tx0) newline = '' +title+'\n' f.writelines(newline) f.writelines(tx1) f.writelines(toc) f.writelines(tx2) newline = '

' + title + '

\n' f.writelines(newline) newline = ''.join(lines[sn+1:en]).replace(old,new) f.writelines(newline) f.writelines(txendl) global N N += 1 print('----- modified successfully') import os def get_path_and_file_name_list(path): list = [] for root, dirs, files in os.walk(path): for file in files: list.append(root+'/'+file) return list path = os.getcwd() files = get_path_and_file_name_list(path) for filename in files: if filename[-5:] == ".html": print(filename) main(filename) print('------------------------------------------') print('All ', N, ' files have been modified successfully')