Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: refactor logging to use loguru and add BlockType and ContentBlock classes for structured content handling

myhloli07db6839b8fd
diff --git a/mineru/model/docx/docx_converter.py b/mineru/model/docx/docx_converter.py
index 237ba2b6..bff15545 100644
--- a/mineru/model/docx/docx_converter.py
+++ b/mineru/model/docx/docx_converter.py
@@ -4,7 +4,6 @@ from io import BytesIO
from pathlib import Path
from typing import BinaryIO, Optional, Union, Any, Final
-import logging
from PIL import Image, WmfImagePlugin
from loguru import logger
from docx import Document
@@ -27,9 +26,6 @@ ACCEPTED_MIME_TYPE_PREFIXES = [
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
]
-ACCEPTED_FILE_EXTENSIONS = [".docx"]
-_log = logging.getLogger(__name__)
-
class DocxConverter:
_BLIP_NAMESPACES: Final = {
@@ -114,7 +110,7 @@ class DocxConverter:
added_elements.extend(t)
except Exception:
# 如果表格解析失败,记录调试信息
- _log.debug("could not parse a table, broken docx table")
+ logger.debug("could not parse a table, broken docx table")
# 检查图片元素
elif drawing_blip:
# 处理图片元素
@@ -141,7 +137,7 @@ class DocxConverter:
# 忽略其他未知元素并记录日志
else:
- _log.debug(f"Ignoring element in DOCX with tag: {tag_name}")
+ logger.debug(f"Ignoring element in DOCX with tag: {tag_name}")
def _handle_tables(self, element: BaseOxmlElement):
"""
@@ -462,7 +458,7 @@ class DocxConverter:
# 使用 PIL 打开 BytesIO 对象创建图像
image_data: Optional[bytes] = get_docx_image(drawing_blip)
if image_data is None:
- _log.warning("Warning: image cannot be found")
+ logger.warning("Warning: image cannot be found")
else:
image_bytes = BytesIO(image_data)
pil_image = Image.open(image_bytes)
@@ -818,7 +814,7 @@ class DocxConverter:
return num_fmt in numbered_formats
except Exception as e:
- _log.debug(f"Error determining if list is numbered: {e}")
+ logger.debug(f"Error determining if list is numbered: {e}")
return False
def _add_list_item(
diff --git a/mineru/model/docx/tools/structs.py b/mineru/model/docx/tools/structs.py
new file mode 100644
index 00000000..61d6d2c2
--- /dev/null
+++ b/mineru/model/docx/tools/structs.py
@@ -0,0 +1,72 @@
+# Copyright (c) Opendatalab. All rights reserved.
+
+class BlockType:
+ TEXT = "text" # 文本
+ TITLE = "title" # 段落标题
+ TABLE = "table" # 表格
+ IMAGE = "image" # 图像
+ HEADER = "header" # 页眉
+ FOOTER = "footer" # 页脚
+ EQUATION = "equation" # 公式(独立公式)
+ LIST = "list" # 列表块(无序/有序列表)
+
+ # captions
+ TABLE_CAPTION = "table_caption" # 表格标题
+ IMAGE_CAPTION = "image_caption" # 图像标题
+
+ UNKNOWN = "unknown" # 未知块
+
+BLOCK_TYPES = set(
+ [
+ BlockType.TEXT,
+ BlockType.TITLE,
+ BlockType.TABLE,
+ BlockType.IMAGE,
+ BlockType.HEADER,
+ BlockType.FOOTER,
+ BlockType.EQUATION,
+ BlockType.TABLE_CAPTION,
+ BlockType.IMAGE_CAPTION,
+ BlockType.LIST,
+ BlockType.UNKNOWN,
+ ]
+)
+
+
+class ContentBlock(dict):
+ def __init__(
+ self,
+ type: str,
+ content: str | None = None,
+ ):
+ """
+ Initialize a layout block.
+ Args:
+ type (str): Type of the block (e.g., 'text', 'image', 'table').
+ content (str or None): The content of the block (if exists).
+ """
+ super().__init__()
+
+ assert type in BLOCK_TYPES, f"Unknown type: {type}"
+ assert content is None or isinstance(content, str), "Content must be a string or None"
+
+ self["type"] = type
+ self["content"] = content
+
+ @property
+ def type(self) -> str:
+ return self["type"]
+
+ @type.setter
+ def type(self, value: str):
+ assert value in BLOCK_TYPES, f"Unknown type: {value}"
+ self["type"] = value
+
+ @property
+ def content(self) -> str | None:
+ return self["content"]
+
+ @content.setter
+ def content(self, value: str | None):
+ assert value is None or isinstance(value, str), "Content must be a string or None"
+ self["content"] = value
\ No newline at end of file