1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import re import os import requests
markdown_file_path = "C:\\Users\\wuyucun\\Desktop\\1_sim_test\\2_md file\\基于ProDiag的监控视图-LAD篇.md"
with open(markdown_file_path, 'r', encoding='utf-8') as file: markdown_content = file.read()
urls = re.findall(r'!\[[^\]]*\]\(([^)]*\.png)\)', markdown_content)
url_list = [] for url in urls: url_list.append(url)
for url in url_list: response = requests.get(url) if response.status_code == 200: image_name = os.path.basename(url)[:255] image_dir = os.path.join(os.path.dirname(markdown_file_path),"picture_libs") image_path = os.path.join(image_dir,image_name) image_relpath = os.path.relpath(image_path,os.path.dirname(markdown_file_path)) with open(image_path, 'wb') as file: file.write(response.content) print(f"图片 {image_name} 下载成功") markdown_content = markdown_content.replace(url, image_relpath) else: print(f"图片 {url} 下载失败") continue
with open(markdown_file_path, 'w', encoding='utf-8') as file: file.write(markdown_content)
print("完成")
|