-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfix_images.py
More file actions
42 lines (33 loc) · 1.14 KB
/
fix_images.py
File metadata and controls
42 lines (33 loc) · 1.14 KB
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
42
#!/usr/bin/env python3
"""修复 HTML 中的图片"""
import re
import sys
html_file = "output/article_20260107_115239.html"
# 读取 HTML
with open(html_file, 'r', encoding='utf-8') as f:
html = f.read()
# 图片路径
cover_image = "images/gemini_20260107_115148_1.png"
inline_images = [
"images/gemini_20260107_115205_1.png",
"images/gemini_20260107_115222_1.png",
"images/gemini_20260107_115239_1.png"
]
# 1. 插入封面图
# 在标题前插入封面
title_pattern = r'(<h1>.*?</h1>)'
cover_html = f'<img src="{cover_image}" class="cover" alt="封面图">\n\n '
html = re.sub(title_pattern, cover_html + r'\1', html, count=1)
# 2. 插入配图
# 在每个 h2 标签后插入图片
h2_pattern = r'(<h2>.*?</h2>)'
h2_tags = re.findall(h2_pattern, html)
for idx, h2 in enumerate(h2_tags[:len(inline_images)]):
img_html = f'\n<img src="{inline_images[idx]}" class="inline-image">'
html = html.replace(h2, h2 + img_html, 1)
# 保存
with open(html_file, 'w', encoding='utf-8') as f:
f.write(html)
print(f"✅ 已修复 {html_file}")
print(f" 插入封面图: 1 张")
print(f" 插入配图: {len(inline_images)} 张")