import os, base64,re
li=os.listdir(r'C:\Users\lenovo\Desktop\苕溪诗切图320jpg')
try:
for i in li:
print(i)
path=os.path.join(r'C:\Users\lenovo\Desktop\苕溪诗切图320jpg',i)
print(path)
ch_path1=os.path.join(r'C:\Users\lenovo\Desktop\苕溪诗切图320png',i.split('.')[0]+'.png')
print(ch_path1)
svg_path = os.path.join(r'C:\Users\lenovo\Desktop\苕溪诗切图320svg',i.split('.')[0]+'.svg')
print(svg_path)
with open(r'C:\Users\lenovo\Desktop\苕溪诗切图320svg\模板.svg', mode='rb') as f:
m=f.read()
s = m.decode('utf-8')
# print(s)
goal=re.findall('base64,(.*?)"',s,re.S)
with open(ch_path1,mode='rb') as f:
imgdata1 = base64.b64encode(f.read())
print(str(imgdata1)[2:-1])
with open(path,mode='rb') as f:
imgdata2 = base64.b64encode(f.read())
print(str(imgdata2)[2:-1])
newsvg=s.replace(goal[1],str(imgdata1)[2:-1]).replace(goal[0],str(imgdata2)[2:-1]).encode('utf-8')
# print(newsvg)
print('ok')
with open(svg_path,mode='wb') as f:
f.write(newsvg)
print('ok')
except:
print('执行结束')
li = os.listdir(r'C:\Users\lenovo\Desktop\苕溪诗切图320svg')
print(li[:-1:1])