fix(parser): 修复 Docling 解析文档时图片错位问题
遍历所有图片生成等长替换列表,非 data URI 图片填空字符串, 确保占位符替换严格 1:1,避免混合图片类型时顺序错乱。
This commit is contained in:
parent
f07b32b365
commit
56166abff6
@ -127,31 +127,24 @@ def _convert_with_docling(file_path: Path, params: dict | None = None) -> str:
|
|||||||
doc = result.document
|
doc = result.document
|
||||||
|
|
||||||
if hasattr(doc, "pictures") and doc.pictures:
|
if hasattr(doc, "pictures") and doc.pictures:
|
||||||
image_refs: list[tuple[str, bytes]] = []
|
replacements: list[str] = []
|
||||||
|
|
||||||
for pic in doc.pictures:
|
for pic in doc.pictures:
|
||||||
if hasattr(pic, "image") and hasattr(pic.image, "uri"):
|
uri = str(pic.image.uri) if hasattr(pic, "image") and hasattr(pic.image, "uri") else ""
|
||||||
uri = str(pic.image.uri)
|
if uri.startswith("data:"):
|
||||||
if uri.startswith("data:"):
|
try:
|
||||||
image_data, mime_type = _parse_data_uri(uri)
|
image_data, mime_type = _parse_data_uri(uri)
|
||||||
timestamp = int(time.time() * 1000000)
|
filename = f"image_{int(time.time() * 1000000)}.{mime_type.split('/')[-1]}"
|
||||||
filename = f"image_{timestamp}.{mime_type.split('/')[-1]}"
|
url = _upload_image_to_minio(image_data, filename, image_bucket, image_prefix)
|
||||||
image_refs.append((filename, image_data))
|
replacements.append(f"")
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
image_urls: list[str] = []
|
logger.error(f"上传图片失败 {filename}: {e}")
|
||||||
for filename, image_data in image_refs:
|
replacements.append("")
|
||||||
try:
|
else:
|
||||||
url = _upload_image_to_minio(image_data, filename, image_bucket, image_prefix)
|
replacements.append("")
|
||||||
image_urls.append(f"")
|
|
||||||
except Exception as e: # noqa: BLE001
|
|
||||||
logger.error(f"上传图片失败 {filename}: {e}")
|
|
||||||
image_urls.append(f"[图片: {filename}]")
|
|
||||||
|
|
||||||
markdown = doc.export_to_markdown()
|
markdown = doc.export_to_markdown()
|
||||||
|
for replacement in replacements:
|
||||||
for url in image_urls:
|
markdown = re.sub(r"<!--\s*image\s*-->", replacement, markdown, count=1)
|
||||||
markdown = re.sub(r"<!--\s*image\s*-->", url, markdown, count=1)
|
|
||||||
|
|
||||||
return markdown
|
return markdown
|
||||||
|
|
||||||
return doc.export_to_markdown()
|
return doc.export_to_markdown()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user