import re import os defparseText(textpath): f = open(textpath,'r+',encoding = 'utf-8') text = str(f.read()) f.close() return text
defgetPicPath(text): #根据你的图片自己设置,不同版本不一样 srcPic = re.findall(r'\!\[image-\d*\]\(.*\)',text) dst = [] for i in srcPic: str1 = re.findall('\/.*\)',i)[0] str1 = str1[1:-1] dst.append(str1) return srcPic,dst
defreplaceText(text,src,dst): for i in range(len(src)): dst[i] = '!['+dst[i]+']('+'https://raw.githubusercontent.com/xxxxx/xxxxx/'+dst[i]+')' #中间的网址替换为https://raw.githubusercontent.com/你的用户名/你的仓库/你的分支/你的目录/ text = text.replace(src[i],dst[i]) return text
defrewrite(textpath,text): f = open(textpath,'w+',encoding='utf-8') f.write(text) f.close()