feat(transit): 支持Markdown图片转Word图片段落

- 新增 transit/images.py 模块处理图片解析和插入逻辑
- 实现 `<img>` 标签解析为独立图片段落
- 添加图片尺寸检测和自适应缩放功能(超出页面宽度时自动缩放)
- 支持图标题居中显示和图片居中对齐
- 优化 body_to_paragraphs 函数,添加图片处理逻辑
- 更新 README.md 使用说明,添加模板文件要求说明

BREAKING CHANGE: 图片处理方式变更,需要包含 sample.docx 模板文件
This commit is contained in:
zzy
2026-05-09 16:17:51 +08:00
parent fc6afdea9d
commit 0b10e97e0c
3 changed files with 200 additions and 33 deletions

View File

@@ -7,9 +7,12 @@ Markdown 正文 → Word 段落转换。
import re
from copy import deepcopy
from docx import Document
from docx.oxml.ns import qn
from .images import make_image_paragraph, is_figure_caption, insert_image_paragraphs
_PAT_HEADING = re.compile(r"^(#{1,6})\s+(.+)$", re.MULTILINE)
@@ -33,17 +36,28 @@ def body_to_paragraphs(
paragraphs: list[dict] = []
last_end = 0
def _add_block(block: str) -> None:
block = block.strip()
if not block:
return
# 图片段落
img = make_image_paragraph(block)
if img:
paragraphs.append(img)
return
# 跳过紧跟在图片后的重复图标题
if paragraphs and paragraphs[-1].get("type") == "image" and is_figure_caption(block):
return
# 普通正文段落
paragraphs.append({"text": block, "level": 0, "style": body_style})
for m in _PAT_HEADING.finditer(md_text):
# 标题前的普通文本
if m.start() > last_end:
pre = md_text[last_end : m.start()].strip()
if pre:
for block in re.split(r"\n\s*\n", pre):
block = block.strip()
if block:
paragraphs.append(
{"text": block, "level": 0, "style": body_style}
)
_add_block(block)
level = len(m.group(1)) + level_offset
heading_text = m.group(2).strip()
@@ -56,11 +70,7 @@ def body_to_paragraphs(
tail = md_text[last_end:].strip()
if tail:
for block in re.split(r"\n\s*\n", tail):
block = block.strip()
if block:
paragraphs.append(
{"text": block, "level": 0, "style": body_style}
)
_add_block(block)
return paragraphs
@@ -94,31 +104,36 @@ def replace_placeholder(
parent.remove(para._element)
for pd_data in reversed(paragraphs):
new_p = doc.add_paragraph(pd_data["text"])
style_name = pd_data["style"]
if pd_data.get("type") == "image":
insert_image_paragraphs(
doc, [pd_data], idx=idx, parent=parent
)
else:
new_p = doc.add_paragraph(pd_data["text"])
style_name = pd_data["style"]
# 尝试应用样式,逐步降级
applied = _apply_style(new_p, doc, style_name)
if not applied and style_name.startswith("Heading"):
new_p.style = doc.styles["Normal"]
elif not applied:
if placeholder_style:
_apply_style(new_p, doc, placeholder_style)
if new_p.style.name == "Normal" and placeholder_style:
new_p.style = doc.styles[placeholder_style]
# 尝试应用样式,逐步降级
applied = _apply_style(new_p, doc, style_name)
if not applied and style_name.startswith("Heading"):
new_p.style = doc.styles["Normal"]
elif not applied:
if placeholder_style:
_apply_style(new_p, doc, placeholder_style)
if new_p.style.name == "Normal" and placeholder_style:
new_p.style = doc.styles[placeholder_style]
# 继承原段落的编号属性(自动编号)
if numPr is not None:
new_pPr = new_p._element.find(qn("w:pPr"))
if new_pPr is None:
new_pPr = new_p._element.makeelement(qn("w:pPr"), {})
new_p._element.insert(0, new_pPr)
existing = new_pPr.find(qn("w:numPr"))
if existing is not None:
new_pPr.remove(existing)
new_pPr.append(deepcopy(numPr))
# 继承原段落的编号属性(自动编号)
if numPr is not None:
new_pPr = new_p._element.find(qn("w:pPr"))
if new_pPr is None:
new_pPr = new_p._element.makeelement(qn("w:pPr"), {})
new_p._element.insert(0, new_pPr)
existing = new_pPr.find(qn("w:numPr"))
if existing is not None:
new_pPr.remove(existing)
new_pPr.append(deepcopy(numPr))
parent.insert(idx, new_p._element)
parent.insert(idx, new_p._element)
break
if not placeholder_found: