Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat: enhance DOCX image handling by refactoring image retrieval and serialization logic
myhloli49f297d1063a
diff --git a/mineru/backend/utils/office_image.py b/mineru/backend/utils/office_image.pyindex 57a9f6aa..3a1217f9 100644--- a/mineru/backend/utils/office_image.py+++ b/mineru/backend/utils/office_image.py@@ -1,5 +1,8 @@# Copyright (c) Opendatalab. All rights reserved.-from PIL import Image, ImageDraw, ImageFont+from io import BytesIO+from pathlib import PurePosixPath++from PIL import Image, ImageDraw, ImageFont, UnidentifiedImageErrorfrom loguru import loggerfrom mineru.utils.check_sys_env import is_windows_environment@@ -7,12 +10,44 @@ from mineru.utils.pdf_reader import image_to_b64strVECTOR_IMAGE_FORMATS = frozenset({"WMF", "EMF"})+VECTOR_IMAGE_EXTENSIONS = frozenset({".wmf", ".emf"})+VECTOR_IMAGE_CONTENT_TYPES = frozenset(+ {+ "image/x-wmf",+ "image/wmf",+ "image/x-emf",+ "image/emf",+ "application/x-msmetafile",+ }+)def is_vector_image(pil_image: Image.Image) -> bool:return (getattr(pil_image, "format", None) or "").upper() in VECTOR_IMAGE_FORMATS+def is_vector_image_part(+ part_name: object | None = None, content_type: str | None = None+) -> bool:+ suffix = PurePosixPath(str(part_name or "")).suffix.lower()+ if suffix in VECTOR_IMAGE_EXTENSIONS:+ return True+ normalized_content_type = (content_type or "").split(";", 1)[0].strip().lower()+ return normalized_content_type in VECTOR_IMAGE_CONTENT_TYPES+++def _vector_image_format_label(+ part_name: object | None = None, content_type: str | None = None+) -> str:+ suffix = PurePosixPath(str(part_name or "")).suffix.lower()+ normalized_content_type = (content_type or "").lower()+ if suffix == ".wmf" or "wmf" in normalized_content_type:+ return "WMF"+ if suffix == ".emf" or "emf" in normalized_content_type:+ return "EMF"+ return "WMF/EMF"++def _load_placeholder_font(font_size: int) -> ImageFont.ImageFont:for font_name in ("DejaVuSans.ttf",@@ -114,3 +149,57 @@ def serialize_vector_image_with_placeholder(placeholder = create_text_placeholder(pil_image.size, placeholder_lines)return image_to_b64str(placeholder, image_format="JPEG")+++def serialize_vector_part_with_placeholder(+ part_name: object | None = None,+ content_type: str | None = None,+ size: tuple[int, int] = (320, 180),+) -> str:+ image_format = _vector_image_format_label(part_name, content_type)+ logger.warning(+ f"Skipping {image_format} image part before Pillow load, "+ f"part_name={part_name}, content_type={content_type}"+ )+ placeholder = create_text_placeholder(+ size,+ [+ f"{image_format} placeholder",+ "Use Windows to parse",+ "the original image",+ ],+ )+ return image_to_b64str(placeholder, image_format="JPEG")+++def serialize_office_image(+ image_data: bytes,+ *,+ part_name: object | None = None,+ content_type: str | None = None,+) -> str | None:+ if is_vector_image_part(part_name, content_type):+ return serialize_vector_part_with_placeholder(part_name, content_type)++ try:+ pil_image = Image.open(BytesIO(image_data))+ pil_image.load()+ except (UnidentifiedImageError, OSError) as e:+ logger.warning(+ f"Warning: image cannot be loaded by Pillow: {e}, "+ f"part_name={part_name}, content_type={content_type}"+ )+ return None++ if is_vector_image(pil_image):+ return serialize_vector_image_with_placeholder(pil_image)++ if pil_image.mode == "RGB":+ return image_to_b64str(pil_image, image_format="JPEG")++ if pil_image.mode in {"RGBA", "LA"} or (+ pil_image.mode == "P" and "transparency" in pil_image.info+ ):+ return image_to_b64str(pil_image.convert("RGBA"), image_format="PNG")++ return image_to_b64str(pil_image.convert("RGB"), image_format="JPEG")diff --git a/mineru/model/docx/docx_converter.py b/mineru/model/docx/docx_converter.pyindex df5dd562..cc788ea6 100644--- a/mineru/model/docx/docx_converter.py+++ b/mineru/model/docx/docx_converter.py@@ -6,7 +6,6 @@ from pathlib import Path, PurePosixPathfrom typing import BinaryIO, Optional, Union, Any, Final, Iteratorfrom zipfile import ZIP_DEFLATED, ZipFile-from PIL import Imagefrom loguru import loggerfrom docx import Documentfrom docx.document import Document as DocxDocument@@ -24,11 +23,9 @@ from mineru.model.docx.tools.math.omml import oMath2Latexfrom mineru.utils.docx_formatting import Formatting, Scriptfrom mineru.utils.enum_class import BlockType, ContentTypefrom mineru.backend.utils.office_image import (- is_vector_image,- serialize_vector_image_with_placeholder,+ serialize_office_image,)from mineru.backend.utils.office_chart import extract_chart_html_from_ooxml-from mineru.utils.pdf_reader import image_to_b64strclass DocxConverter:_BLIP_NAMESPACES: Final = {@@ -1260,24 +1257,22 @@ class DocxConverter:)return rel_id- def get_docx_image(image: Any) -> Optional[bytes]:+ def get_docx_image_part(image: Any) -> Optional[Any]:"""- 获取 DOCX 图像数据。+ 获取 DOCX 图像 part。Args:image: 单个 blip 元素Returns:- Optional[bytes]: 图像数据+ Optional[Any]: 图像 part"""- image_data: Optional[bytes] = NonerId = get_docx_image_rel_id(image)if rId in self.docx_obj.part.rels:# 使用关系 ID 访问图像部分- image_part = self.docx_obj.part.rels[rId].target_part- image_data = image_part.blob # 获取二进制图像数据- return image_data+ return self.docx_obj.part.rels[rId].target_part+ return Noneseen_rel_ids: set[str] = set()# 遍历所有图片引用元素,支持 DrawingML blip 和 VML imagedata。@@ -1287,27 +1282,24 @@ class DocxConverter:continueif rel_id:seen_rel_ids.add(rel_id)- image_data: Optional[bytes] = get_docx_image(image)- if image_data is None:+ image_part = get_docx_image_part(image)+ if image_part is None:logger.warning("Warning: image cannot be found")- else:- image_bytes = BytesIO(image_data)- pil_image = Image.open(image_bytes)- if is_vector_image(pil_image):- img_base64 = serialize_vector_image_with_placeholder(pil_image)- else:- # 处理常规图片- if pil_image.mode != "RGB":- # RGBA, P, L 等模式保留原貌并存为 PNG (PNG支持透明度)- img_base64 = image_to_b64str(pil_image, image_format="PNG")- else:- # 纯 RGB 图片存为 JPEG 以减小体积- img_base64 = image_to_b64str(pil_image, image_format="JPEG")- image_block = {- "type": BlockType.IMAGE,- "content": img_base64,- }- self.cur_page.append(image_block)+ continue++ img_base64 = serialize_office_image(+ image_part.blob,+ part_name=getattr(image_part, "partname", None),+ content_type=getattr(image_part, "content_type", None),+ )+ if img_base64 is None:+ continue++ image_block = {+ "type": BlockType.IMAGE,+ "content": img_base64,+ }+ self.cur_page.append(image_block)def _get_paragraph_elements(self, paragraph: Paragraph):"""@@ -1964,6 +1956,8 @@ class DocxConverter:content_text = self._build_text_with_equations_and_hyperlinks(elements, text, equations)+ if content_text == "":+ return None# 确定列表属性list_attribute = "ordered" if is_numbered else "unordered"@@ -2012,6 +2006,22 @@ class DocxConverter:"ilevel": ilevel,}+ if not self.list_block_stack:+ logger.warning(+ "Missing DOCX list parent for increased indent; "+ f"numid={numid}, ilevel={ilevel}. Starting a new list block."+ )+ self.cur_page.append(child_list_block)+ self.list_block_stack.append(child_list_block)+ child_list_block["content"].append(+ {+ "type": BlockType.TEXT,+ "content": content_text,+ }+ )+ self.pre_ilevel = ilevel+ return None+# 获取栈顶的列表块,将子列表直接添加到其content中parent_list_block = self.list_block_stack[-1]parent_list_block["content"].append(child_list_block)@@ -2041,7 +2051,21 @@ class DocxConverter:if top_list_block["ilevel"] == ilevel:breakself.list_block_stack.pop()- list_block = self.list_block_stack[-1]+ if not self.list_block_stack:+ logger.warning(+ "Malformed DOCX list nesting; "+ f"numid={numid}, ilevel={ilevel}. Starting a new list block."+ )+ list_block = {+ "type": BlockType.LIST,+ "attribute": list_attribute,+ "content": [],+ "ilevel": ilevel,+ }+ self.cur_page.append(list_block)+ self.list_block_stack.append(list_block)+ else:+ list_block = self.list_block_stack[-1]list_item = {"type": BlockType.TEXT,@@ -2052,9 +2076,22 @@ class DocxConverter:# 情况 4: 同级列表项(相同缩进)elif self.pre_num_id == numid and self.pre_ilevel == ilevel:- # 获取栈顶的列表块- list_block = self.list_block_stack[-1]-+ if not self.list_block_stack:+ logger.warning(+ "Missing DOCX list block for same indent; "+ f"numid={numid}, ilevel={ilevel}. Starting a new list block."+ )+ list_block = {+ "type": BlockType.LIST,+ "attribute": list_attribute,+ "content": [],+ "ilevel": ilevel,+ }+ self.cur_page.append(list_block)+ self.list_block_stack.append(list_block)+ else:+ # 获取栈顶的列表块+ list_block = self.list_block_stack[-1]list_item = {"type": BlockType.TEXT,