feat(shipping-notice): 添加 MinerU 订单附件表格结构化功能,更新环境配置和数据模型以支持新布局解析和文档模板管理

This commit is contained in:
hugo committed 2026-07-22 18:52:43 +08:00
1 parent c4095b08da
commit c35223d49d
148 files changed
+15709 -1353

No files matched your search

@@ -2,7 +2,7 @@
services/shipping_notice/eml_parse.py
用途:将 .eml 解析为结构化结果(邮件元数据、清洗正文、真实附件及各自文本)。
依赖:标准库 email/html;Flask current_app(可选);notice_upload.extract_notice_text(叶附件抽取)。
依赖:标准库 email/html、BeautifulSoup;Flask current_app(可选);notice_upload.extract_notice_text(叶附件抽取)。
逻辑:由 notice_upload.extract_notice_text 在 file_type=eml 时调用;忽略内嵌图片/签名图;
嵌套 message/rfc822 与附件型 multipart(disposition/filename)登记为独立附件;
外层 body 不穿透 rfc822 / 附件型 multipart,避免订单文本误入正文或重复;
@@ -24,11 +24,15 @@ from email.utils import parsedate_to_datetime
from pathlib import Path
from typing import Any, Callable
from bs4 import BeautifulSoup, Comment
# 签名/Logo 文件名启发式(仅对 image/* 生效)
_SIGNATURE_LOGO_NAME_RE = re.compile(
r"(?:^|[_.\-])(logo|signature|sig|banner|icon|spacer|pixel|footer|header)(?:[_.\-]|$)",
re.IGNORECASE,
)
_HTML_TABLE_TAGS = frozenset({'table', 'thead', 'tbody', 'tfoot', 'tr', 'th', 'td'})
_HTML_DROP_TAGS = frozenset({'script', 'style', 'img', 'svg', 'object', 'embed', 'iframe', 'template'})
@dataclass
@@ -330,6 +334,52 @@ def _extract_body_text(msg: Message) -> str:
return plain or html
def _extract_body_html_tables(msg: Message) -> list[str]:
"""
提取根邮件正文中的 HTML 表格并做最小白名单清洗。
核心逻辑:即使 multipart/alternative 同时存在 plain,也读取对应 HTML 的 table;
仅保留表格结构、cell 文本及 rowspan/colspan,禁止脚本、样式、链接和图片进入布局快照。
Args:
msg: 当前 EML 根消息;不穿透 rfc822 或附件型 multipart。
Returns:
最多 20 张、合计不超过 200000 字符的安全 HTML table 片段。
"""
result: list[str] = []
total_chars = 0
for part in _iter_local_body_candidates(msg):
if str(part.get_content_type() or '').lower() != 'text/html':
continue
raw = _decode_part_text(part, _read_part_payload(part))
soup = BeautifulSoup(raw or '', 'html.parser')
for table in soup.find_all('table'):
if table.find_parent('table') is not None:
continue
fragment = BeautifulSoup(str(table), 'html.parser')
for comment in fragment.find_all(string=lambda value: isinstance(value, Comment)):
comment.extract()
for tag in list(fragment.find_all(True)):
if tag.name in _HTML_DROP_TAGS:
tag.decompose()
continue
if tag.name not in _HTML_TABLE_TAGS:
tag.unwrap()
continue
allowed = {'rowspan', 'colspan'} if tag.name in {'th', 'td'} else set()
tag.attrs = {
key: value for key, value in tag.attrs.items()
if str(key).lower() in allowed
}
cleaned = str(fragment.find('table') or '').strip()
if not cleaned or total_chars + len(cleaned) > 200_000 or len(result) >= 20:
continue
result.append(cleaned)
total_chars += len(cleaned)
break
return result
def _is_collectible_text_leaf(msg: Message) -> bool:
"""判定部件是否可作为附件容器内的可落库文本叶子(非 filename 附件)。"""
ctype = str(msg.get_content_type() or "").lower()
@@ -1126,6 +1176,7 @@ def parse_eml_bytes(
date_val = _format_date(msg)
message_id = _normalize_message_id(_header_str(msg, "Message-ID") or _header_str(msg, "Message-Id"))
body_text = _extract_body_text(msg)
body_html_tables = _extract_body_html_tables(msg)
state = _ParseState(limits=limits, extract_text=extract_text)
_walk_message(msg, state, depth=depth)
@@ -1143,6 +1194,7 @@ def parse_eml_bytes(
"to": to_addr,
"date": date_val,
"body_text": combined_body,
"body_html_tables": body_html_tables,
}
flat = _compose_flat_text(
subject=subject,