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 processing by adding main execution block and improving text content handling with equations
myhlolie7c67a95b616
diff --git a/mineru/backend/office/docx_analyze.py b/mineru/backend/office/docx_analyze.pyindex 39577be4..78152445 100644--- a/mineru/backend/office/docx_analyze.py+++ b/mineru/backend/office/docx_analyze.py@@ -26,4 +26,18 @@ def office_docx_analyze(image_writer,)- return middle_json, results\ No newline at end of file+ return middle_json, results++if __name__ == '__main__':+ docx_path = "/Users/myhloli/projects/20240809magic_pdf/Magic-PDF/mineru/model/docx/test.docx"+ from mineru.data.data_reader_writer import FileBasedDataWriter+ with open(docx_path, 'rb') as f:+ file_bytes = f.read()+ image_writer = FileBasedDataWriter("./output_images")+ middle_json, results = office_docx_analyze(+ file_bytes,+ image_writer=image_writer,+ )++ import json+ print(json.dumps(middle_json, indent=2, ensure_ascii=False))\ No newline at end of filediff --git a/mineru/backend/office/office_magic_model.py b/mineru/backend/office/office_magic_model.pyindex 6a98ae90..c547318c 100644--- a/mineru/backend/office/office_magic_model.py+++ b/mineru/backend/office/office_magic_model.py@@ -17,36 +17,10 @@ class MagicModel:self.all_spans = []# 解析每个块for index, block_info in enumerate(page_blocks):- block_bbox = block_info["bbox"]- try:- x1, y1, x2, y2 = block_bbox- x_1, y_1, x_2, y_2 = (- int(x1 * width),- int(y1 * height),- int(x2 * width),- int(y2 * height),- )- if x_2 < x_1:- x_1, x_2 = x_2, x_1- if y_2 < y_1:- y_1, y_2 = y_2, y_1- block_bbox = (x_1, y_1, x_2, y_2)- block_type = block_info["type"]- block_content = block_info["content"]- block_angle = block_info["angle"]-- # print(f"坐标: {block_bbox}")- # print(f"类型: {block_type}")- # print(f"内容: {block_content}")- # print("-" * 50)- except Exception as e:- # 如果解析失败,可能是因为格式不正确,跳过这个块- logger.warning(f"Invalid block format: {block_info}, error: {e}")- continue+ block_type = block_info["type"]+ block_content = block_info["content"]span_type = "unknown"- code_block_sub_type = None- guess_lang = Noneif block_type in ["text",@@ -72,12 +46,6 @@ class MagicModel:elif block_type in ["table"]:block_type = BlockType.TABLE_BODYspan_type = ContentType.TABLE- elif block_type in ["code", "algorithm"]:- block_content = code_content_clean(block_content)- code_block_sub_type = block_type- block_type = BlockType.CODE_BODY- span_type = ContentType.TEXT- guess_lang = guess_language_by_text(block_content)elif block_type in ["equation"]:block_type = BlockType.INTERLINE_EQUATIONspan_type = ContentType.INTERLINE_EQUATIONdiff --git a/mineru/model/docx/docx_converter.py b/mineru/model/docx/docx_converter.pyindex 25d4b3a0..853f29fb 100644--- a/mineru/model/docx/docx_converter.py+++ b/mineru/model/docx/docx_converter.py@@ -202,6 +202,8 @@ class DocxConverter:ilevel=ilevel,elements=paragraph_elements,is_numbered=is_numbered,+ text=text,+ equations=equations,)# 列表项已处理,返回return None@@ -689,6 +691,27 @@ class DocxConverter:logger.debug(f"Error determining if list is numbered: {e}")return False+ def _build_text_content_list_with_equations(+ self, text: str, equations: list+ ) -> list:+ """+ 构建包含行内公式的 text_content_list。++ Args:+ text: 处理后的文本(包含公式标记,如 <eq>...</eq>)+ equations: 公式列表++ Returns:+ list: text_content_list 内容列表+ """+ # 直接返回包含 <eq></eq> 标记的文本内容,与其他 text/title 保持一致+ return [+ {+ "type": ContentType.TEXT,+ "content": text,+ }+ ]+def _add_list_item(self,*,@@ -696,6 +719,8 @@ class DocxConverter:ilevel: int,elements: list,is_numbered: bool = False,+ text: str = "",+ equations: list = None,) -> list:"""添加列表项。@@ -706,10 +731,14 @@ class DocxConverter:ilevel: 缩进等级elements: 元素列表is_numbered: 是否编号+ text: 处理后的文本(包含公式标记)+ equations: 公式列表Returns:list[RefItem]: 元素引用列表"""+ if equations is None:+ equations = []elem_ref: list = []if not elements:return elem_ref@@ -725,9 +754,7 @@ class DocxConverter:list_block = {"type": BlockType.LIST,"attribute": list_attribute,- "list_nest_level": 1,"list_items": [],- "list_type": "text_list","ilevel": ilevel,}self.cur_page.append(list_block)@@ -735,27 +762,18 @@ class DocxConverter:self.list_block_stack.append(list_block)# 记录当前引用elem_ref.append(id(list_block))- elem_text = ""- for text, format, hyperlink in elements:- elem_text += text- if is_numbered:- counter = self._get_list_counter(numid, ilevel)- enum_marker = str(counter) + "."- else:- enum_marker = ""+ # 构建 text_content_list,处理行内公式+ text_content_list = self._build_text_content_list_with_equations(+ text, equations+ )list_item = {"item_type": "text","item_content": [{"type": BlockType.TEXT,- "text_content_list": [- {- "type": ContentType.TEXT,- "content": enum_marker + elem_text,- }- ],+ "text_content_list": text_content_list,}],}@@ -774,17 +792,13 @@ class DocxConverter:list_attribute = "ordered"else:list_attribute = "unordered"+list_block = {"type": BlockType.LIST,"attribute": list_attribute,- "list_nest_level": 1,"list_items": [],- "list_type": "text_list","ilevel": ilevel,}- # 增加目前栈内的 nest level- for block in self.list_block_stack:- block["list_nest_level"] += 1# 获取栈顶的列表块parent_list_block = self.list_block_stack[-1]# 将新列表块添加为父列表块的最新列表项的子块@@ -795,27 +809,17 @@ class DocxConverter:self.list_block_stack.append(list_block)elem_ref.append(id(list_block))- # 创建列表项- elem_text = ""- for text, format, hyperlink in elements:- elem_text += text+ # 构建 text_content_list,处理行内公式+ text_content_list = self._build_text_content_list_with_equations(+ text, equations+ )- if is_numbered:- counter = self._get_list_counter(numid, ilevel)- enum_marker = str(counter) + "."- else:- enum_marker = ""list_item = {"item_type": "text","item_content": [{"type": BlockType.TEXT,- "text_content_list": [- {- "type": ContentType.TEXT,- "content": enum_marker + elem_text,- }- ],+ "text_content_list": text_content_list,}],}@@ -837,26 +841,17 @@ class DocxConverter:self.list_block_stack.pop()list_block = self.list_block_stack[-1]- elem_text = ""- for text, format, hyperlink in elements:- elem_text += text- if is_numbered:- counter = self._get_list_counter(numid, ilevel)- enum_marker = str(counter) + "."- else:- enum_marker = ""+ # 构建 text_content_list,处理行内公式+ text_content_list = self._build_text_content_list_with_equations(+ text, equations+ )list_item = {"item_type": "text","item_content": [{"type": BlockType.TEXT,- "text_content_list": [- {- "type": ContentType.TEXT,- "content": enum_marker + elem_text,- }- ],+ "text_content_list": text_content_list,}],}@@ -868,27 +863,18 @@ class DocxConverter:elif self.pre_num_id == numid or self.pre_ilevel == ilevel:# 获取栈顶的列表块list_block = self.list_block_stack[-1]- elem_text = ""- for text, format, hyperlink in elements:- elem_text += text- if is_numbered:- counter = self._get_list_counter(numid, ilevel)- enum_marker = str(counter) + "."- else:- enum_marker = ""+ # 构建 text_content_list,处理行内公式+ text_content_list = self._build_text_content_list_with_equations(+ text, equations+ )list_item = {"item_type": "text","item_content": [{"type": BlockType.TEXT,- "text_content_list": [- {- "type": ContentType.TEXT,- "content": enum_marker + elem_text,- }- ],+ "text_content_list": text_content_list,}],}diff --git a/mineru/model/docx/test.docx b/mineru/model/docx/test.docxindex 87396ce2..e95cb0f1 100644Binary files a/mineru/model/docx/test.docx and b/mineru/model/docx/test.docx differ