md2pdf技能支持Markdown表格渲染

This commit is contained in:
2026-09-04 15:59:23 +08:00
parent e150e9c6c9
commit e3972fb25a
2 changed files with 72 additions and 5 deletions
+3 -2
View File
@@ -18,10 +18,11 @@ output_schema:
## 功能说明 ## 功能说明
- 该技能使用 `reportlab` 将 Markdown 转换为 PDF。 - 该技能使用 `reportlab` 将 Markdown 转换为 PDF。
- 支持标题、加粗、斜体、列表、引用、分隔线、图片等常用 Markdown 语法 - 支持标题、加粗、斜体、列表、引用、分隔线、图片、表格等常用 Markdown 语法
- 图片:`![alt](src)` 独立成行时嵌入 PDF(居中,超宽时等比缩到版心宽); - 图片:`![alt](src)` 独立成行时嵌入 PDF(居中,超宽时等比缩到版心宽);
相对路径按 **Markdown 文件所在目录** 解析(如任务目录里的图表),也支持绝对路径与 http(s) 图片; 相对路径按 **Markdown 文件所在目录** 解析(如任务目录里的图表),也支持绝对路径与 http(s) 图片;
图片加载失败退回 alt 文本占位,不会静默丢失;表格语法暂不支持 图片加载失败退回 alt 文本占位,不会静默丢失
- 表格:`| a | b |` 管道表格(含对齐分隔行)自动识别为表格,表头加底纹、隔行斑马纹、居中显示
- 自动处理中文字体 - 自动处理中文字体
## 调用方式 ## 调用方式
+69 -3
View File
@@ -13,13 +13,15 @@ from pathlib import Path
from reportlab.lib.pagesizes import A4 from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm from reportlab.lib.units import mm
from reportlab.lib import colors
from reportlab.lib.styles import ParagraphStyle from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.enums import TA_LEFT from reportlab.lib.enums import TA_LEFT
from reportlab.lib.utils import ImageReader from reportlab.lib.utils import ImageReader
from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont from reportlab.pdfbase.ttfonts import TTFont
from reportlab.platypus import ( from reportlab.platypus import (
SimpleDocTemplate, Paragraph, Spacer, ListFlowable, ListItem, HRFlowable, Image SimpleDocTemplate, Paragraph, Spacer, ListFlowable, ListItem, HRFlowable, Image,
Table, TableStyle
) )
# ---------- 字体注册 ---------- # ---------- 字体注册 ----------
@@ -194,6 +196,58 @@ def image_flowable(src, md_dir, max_width):
img.hAlign = 'CENTER' img.hAlign = 'CENTER'
return img return img
# ---------- 表格 ----------
# 数据/表头行:| a | b |;分隔行:| --- | :---: | ---: | --- |
TABLE_ROW_RE = re.compile(r'^\|.*\|\s*$')
TABLE_SEP_RE = re.compile(r'^\|?\s*:?-{3,}:?\s*(?:\|\s*:?-{3,}:?\s*)+\|?\s*$')
def split_row(line):
"""按未转义的竖线拆分表格行,保留 `\|` 转义出来的字面竖线"""
line = line.strip()
cells = re.split(r'(?<!\\)\|', line)
cells = [c.replace('\\|', '|').strip() for c in cells]
# 首尾的空单元格(来自行首/行尾的 |)去掉;全空则删掉
if cells and cells[0] == '': cells = cells[1:]
if cells and cells[-1] == '': cells = cells[:-1]
return cells
def table_flowable(rows, max_width, styles):
"""表格行 → platypus Table(首行为表头加底纹,超宽时分列撑满版心)"""
ncols = max(len(r) for r in rows)
colw = max_width / ncols
cell_style = ParagraphStyle(
name='TableCell',
fontName=styles['body'].fontName,
fontSize=8.5, leading=12,
leftIndent=3, rightIndent=3,
alignment=TA_LEFT,
)
header_style = ParagraphStyle(
name='TableHeader', parent=cell_style,
fontName=styles['h3'].fontName, # 粗体
textColor=colors.white,
)
data = []
for ri, row in enumerate(rows):
row = row + [''] * (ncols - len(row))
data.append([
Paragraph(render_inline(c), header_style if ri == 0 else cell_style)
for c in row
])
t = Table(data, colWidths=[colw] * ncols)
t.setStyle(TableStyle([
('GRID', (0, 0), (-1, -1), 0.5, colors.HexColor('#c9d4e4')),
('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#2c3e50')),
('ROWBACKGROUNDS', (0, 1), (-1, -1), [colors.white, colors.HexColor('#f4f7fb')]),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('LEFTPADDING', (0, 0), (-1, -1), 4),
('RIGHTPADDING', (0, 0), (-1, -1), 4),
('TOPPADDING', (0, 0), (-1, -1), 3),
('BOTTOMPADDING', (0, 0), (-1, -1), 3),
]))
t.hAlign = 'CENTER'
return t
# ---------- Markdown 解析主函数 ---------- # ---------- Markdown 解析主函数 ----------
def parse_markdown(md_text, styles, md_dir=None, max_width=170 * mm): def parse_markdown(md_text, styles, md_dir=None, max_width=170 * mm):
"""解析 Markdown 文本,返回 platypus 故事列表""" """解析 Markdown 文本,返回 platypus 故事列表"""
@@ -218,8 +272,11 @@ def parse_markdown(md_text, styles, md_dir=None, max_width=170 * mm):
current_list = [] current_list = []
current_list_type = None current_list_type = None
for raw_line in lines: i = 0
line = raw_line.strip() n = len(lines)
while i < n:
line = lines[i].strip()
i += 1
if not line: if not line:
flush_list() flush_list()
continue continue
@@ -276,6 +333,15 @@ def parse_markdown(md_text, styles, md_dir=None, max_width=170 * mm):
elif line.startswith('> '): elif line.startswith('> '):
flush_list() flush_list()
story.append(Paragraph(render_inline(line[2:]), styles["quote"])) story.append(Paragraph(render_inline(line[2:]), styles["quote"]))
# 表格:当前行是表头行,且下一行是分隔行 → 收拢成整张表
elif TABLE_ROW_RE.match(line) and i < n and TABLE_SEP_RE.match(lines[i].strip()):
flush_list()
i += 1 # 跳过分隔行
rows = [split_row(line)]
while i < n and TABLE_ROW_RE.match(lines[i].strip()):
rows.append(split_row(lines[i].strip()))
i += 1
story.append(table_flowable(rows, max_width, styles))
# 普通段落 # 普通段落
else: else:
flush_list() flush_list()