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: add support for chart blocks in hybrid model output and update related processing functions
myhloli8924d922f015
diff --git a/docs/en/reference/output_files.md b/docs/en/reference/output_files.mdindex 0dc7a60c..7c60c73b 100644--- a/docs/en/reference/output_files.md+++ b/docs/en/reference/output_files.md@@ -8,6 +8,7 @@ The exact set of generated files depends on the backend and the input document t- **Visual debugging files**: Help users intuitively understand the document parsing process and results- **Structured data files**: Contain detailed parsing data for secondary development+- In multimodal markdown output, `image` / `chart` blocks render the screenshot first; when `content` exists, a collapsed HTML `<details>` block is appended after the image, using the block `sub_type` as the summary label when available and falling back to `image content` or `chart content`The following sections provide detailed descriptions of each file's purpose and format.@@ -133,7 +134,7 @@ The following sections provide detailed descriptions of each file's purpose and##### Block Structure Hierarchy```-Level 1 blocks (table | image)+Level 1 blocks (table | image | chart)└── Level 2 blocks└── Lines└── Spans@@ -143,7 +144,7 @@ Level 1 blocks (table | image)| Field Name | Description ||------------|-------------|-| `type` | Block type: `table` or `image` |+| `type` | Block type: `table`, `image`, or `chart` || `bbox` | Rectangular box coordinates of the block `[x0, y0, x1, y1]` || `blocks` | List of contained level 2 blocks |@@ -165,6 +166,9 @@ Level 1 blocks (table | image)| `table_body` | Table body || `table_caption` | Table caption text || `table_footnote` | Table footnote |+| `chart_body` | Chart body |+| `chart_caption` | Chart caption text |+| `chart_footnote` | Chart footnote || `text` | Text block || `title` | Title block || `index` | Index block |@@ -179,8 +183,8 @@ Level 1 blocks (table | image)**Span fields**:- `bbox`: Rectangular box coordinates of the span-- `type`: Span type (`image`, `table`, `text`, `inline_equation`, `interline_equation`)-- `content` | `img_path`: Text content or image path+- `type`: Span type (`image`, `table`, `chart`, `text`, `inline_equation`, `interline_equation`)+- `content` | `image_path`: Text content or image path##### Sample Data@@ -322,6 +326,7 @@ Text levels are distinguished through the `text_level` field:- All content blocks include a `bbox` field representing the bounding box coordinates of the content block `[x0, y0, x1, y1]`, mapped to a range of 0-1000.- `code` entries use `sub_type` to distinguish `code` and `algorithm`, and may include fields such as `code_body`, `code_caption`, and `code_footnote`.- `list` entries may use `sub_type` to distinguish ordinary lists from reference-style lists.+- `image` / `chart` entries may include an optional `sub_type` field to carry the visual subtype through downstream outputs.##### Sample Data@@ -409,6 +414,8 @@ Text levels are distinguished through the `text_level` field:| `bbox` | `list[int]` | Optional bounding box mapped into the 0-1000 coordinate range || `anchor` | `string` | Optional anchor; some `DOCX` titles or index items may include it |+`image` / `chart` items may also include an optional top-level `sub_type` field for visual subtype propagation.+##### Common Types| Type | Description |@@ -654,6 +661,8 @@ Based on the pipeline format, with these VLM-specific extensions:* Fields: `code_body` (string), optional `code_caption` (list of strings)- New `list` type with `sub_type` (`text` | `ref_text`):* Field: `list_items` (array of strings)+- `image` / `chart` entries may carry an optional `sub_type` field for visual subtype propagation.+- `chart` entries may additionally expose `content`, `chart_caption`, and `chart_footnote` alongside `img_path`; `content` preserves the original Markdown table text.- All `discarded_blocks` entries are also output (e.g., headers, footers, page numbers, margin notes, page footnotes).- Existing types (`image`, `table`, `text`, `equation`) remain unchanged.- `bbox` still uses the 0–1000 normalized coordinate mapping.diff --git a/docs/zh/reference/output_files.md b/docs/zh/reference/output_files.mdindex 01d92abb..0d8a0505 100644--- a/docs/zh/reference/output_files.md+++ b/docs/zh/reference/output_files.md@@ -8,6 +8,7 @@- **可视化调试文件**:帮助用户直观了解文档解析过程和结果- **结构化数据文件**:包含详细的解析数据,可用于二次开发+- 多模态 markdown 输出中,`image` / `chart` 默认以截图为主;若块内存在 `content`,会在图片后追加一个默认折叠的 HTML `<details>` 内容块,其中折叠标题优先使用块的 `sub_type`,否则回退为 `image content` 或 `chart content`下面将详细介绍每个文件的作用和格式。@@ -133,7 +134,7 @@##### 块结构层次```-一级块 (table | image)+一级块 (table | image | chart)└── 二级块└── 行 (line)└── 片段 (span)@@ -143,7 +144,7 @@| 字段名 | 说明 ||--------|------|-| `type` | 块类型:`table` 或 `image` |+| `type` | 块类型:`table`、`image` 或 `chart` || `bbox` | 块的矩形框坐标 `[x0, y0, x1, y1]` || `blocks` | 包含的二级块列表 |@@ -165,6 +166,9 @@| `table_body` | 表格本体 || `table_caption` | 表格描述文本 || `table_footnote` | 表格脚注 |+| `chart_body` | 图表本体 |+| `chart_caption` | 图表描述文本 |+| `chart_footnote` | 图表脚注 || `text` | 文本块 || `title` | 标题块 || `index` | 目录块 |@@ -179,8 +183,8 @@**片段 (span) 字段**:- `bbox`:片段的矩形框坐标-- `type`:片段类型(`image`、`table`、`text`、`inline_equation`、`interline_equation`)-- `content` | `img_path`:文本内容或图片路径+- `type`:片段类型(`image`、`table`、`chart`、`text`、`inline_equation`、`interline_equation`)+- `content` | `image_path`:文本内容或图片路径##### 示例数据@@ -322,6 +326,7 @@- 所有内容块都包含 `bbox` 字段,表示内容块的边界框坐标 `[x0, y0, x1, y1]` 映射在0-1000范围内的结果。- `code` 类型会通过 `sub_type` 区分 `code` 和 `algorithm`,并可包含 `code_body`、`code_caption`、`code_footnote` 等字段。- `list` 类型可通过 `sub_type` 区分普通列表和参考文献列表。+- `image` / `chart` 类型可包含可选 `sub_type` 字段,用于透传视觉子类型。##### 示例数据@@ -409,6 +414,8 @@| `bbox` | `list[int]` | 可选,0-1000 范围的边界框 || `anchor` | `string` | 可选,部分 `DOCX` 标题或索引项会携带锚点 |+其中 `image` / `chart` 类型还可能包含可选顶层字段 `sub_type`,用于表示视觉子类型。+##### 常见类型| 类型 | 说明 |@@ -758,6 +765,9 @@ vlm 后端的 content_list.json 文件结构与 pipeline 后端类似,伴随* `text`* `ref_text`+- `image` / `chart` 类型可能带有可选 `sub_type` 字段,用于透传视觉子类型+- `chart` 类型除 `img_path` 外,还可包含 `content`、`chart_caption`、`chart_footnote`,其中 `content` 保持原始 Markdown 表格文本+- 增加所有所有`discarded_blocks`的输出内容* `header`* `footer`diff --git a/mineru/backend/hybrid/hybrid_model_output_to_middle_json.py b/mineru/backend/hybrid/hybrid_model_output_to_middle_json.pyindex 67b85bcf..d4637e9b 100644--- a/mineru/backend/hybrid/hybrid_model_output_to_middle_json.py+++ b/mineru/backend/hybrid/hybrid_model_output_to_middle_json.py@@ -67,6 +67,7 @@ def blocks_to_page_info()image_blocks = magic_model.get_image_blocks()table_blocks = magic_model.get_table_blocks()+ chart_blocks = magic_model.get_chart_blocks()title_blocks = magic_model.get_title_blocks()discarded_blocks = magic_model.get_discarded_blocks()code_blocks = magic_model.get_code_blocks()@@ -103,9 +104,9 @@ def blocks_to_page_info(interline_equation_blocks = magic_model.get_interline_equation_blocks()all_spans = magic_model.get_all_spans()- # 对image/table/interline_equation的span截图+ # 对image/table/chart/interline_equation的span截图for span in all_spans:- if span["type"] in [ContentType.IMAGE, ContentType.TABLE, ContentType.INTERLINE_EQUATION]:+ if span["type"] in [ContentType.IMAGE, ContentType.TABLE, ContentType.CHART, ContentType.INTERLINE_EQUATION]:span = cut_image_and_table(span, page_pil_img, page_img_md5, page_index, image_writer, scale=scale)replace_inline_table_images(table_blocks, image_writer, page_index)@@ -114,6 +115,7 @@ def blocks_to_page_info(page_blocks.extend([*image_blocks,*table_blocks,+ *chart_blocks,*code_blocks,*ref_text_blocks,*phonetic_blocks,diff --git a/mineru/backend/vlm/model_output_to_middle_json.py b/mineru/backend/vlm/model_output_to_middle_json.pyindex 0871d829..63e3b877 100644--- a/mineru/backend/vlm/model_output_to_middle_json.py+++ b/mineru/backend/vlm/model_output_to_middle_json.py@@ -47,6 +47,7 @@ def blocks_to_page_info(page_blocks, image_dict, page, image_writer, page_index)magic_model = MagicModel(page_blocks, width, height)image_blocks = magic_model.get_image_blocks()table_blocks = magic_model.get_table_blocks()+ chart_blocks = magic_model.get_chart_blocks()title_blocks = magic_model.get_title_blocks()discarded_blocks = magic_model.get_discarded_blocks()code_blocks = magic_model.get_code_blocks()@@ -73,9 +74,9 @@ def blocks_to_page_info(page_blocks, image_dict, page, image_writer, page_index)interline_equation_blocks = magic_model.get_interline_equation_blocks()all_spans = magic_model.get_all_spans()- # 对image/table/interline_equation的span截图+ # 对image/table/chart/interline_equation的span截图for span in all_spans:- if span["type"] in [ContentType.IMAGE, ContentType.TABLE, ContentType.INTERLINE_EQUATION]:+ if span["type"] in [ContentType.IMAGE, ContentType.TABLE, ContentType.CHART, ContentType.INTERLINE_EQUATION]:span = cut_image_and_table(span, page_pil_img, page_img_md5, page_index, image_writer, scale=scale)replace_inline_table_images(table_blocks, image_writer, page_index)@@ -84,6 +85,7 @@ def blocks_to_page_info(page_blocks, image_dict, page, image_writer, page_index)page_blocks.extend([*image_blocks,*table_blocks,+ *chart_blocks,*code_blocks,*ref_text_blocks,*phonetic_blocks,diff --git a/mineru/backend/vlm/vlm_middle_json_mkcontent.py b/mineru/backend/vlm/vlm_middle_json_mkcontent.pyindex 3b906b9f..32ef74f8 100644--- a/mineru/backend/vlm/vlm_middle_json_mkcontent.py+++ b/mineru/backend/vlm/vlm_middle_json_mkcontent.py@@ -1,6 +1,6 @@import osimport re-from html import unescape+from html import escape, unescapefrom loguru import logger@@ -56,6 +56,90 @@ def _format_embedded_html(html, img_buket_path):return _replace_eq_tags_in_table_html(_prefix_table_img_src(html, img_buket_path))+def _build_media_path(img_buket_path, image_path):+ if not image_path:+ return ''+ if not img_buket_path:+ return image_path+ return f"{img_buket_path}/{image_path}"+++def _apply_visual_sub_type(para_content, para_block):+ sub_type = para_block.get('sub_type')+ if sub_type:+ para_content['sub_type'] = sub_type+++def _build_visual_details_block(content, span_type, summary_override=''):+ if not isinstance(content, str) or not content.strip():+ return ''++ if span_type == ContentType.CHART:+ summary = summary_override or "chart content"+ details_body = content+ else:+ summary = summary_override or "image content"+ details_body = f"<pre>{escape(content)}</pre>"++ return (+ "<details>\n"+ f"<summary>{summary}</summary>\n\n"+ f"{details_body}\n"+ "</details>"+ )+++def _build_visual_body_markdown(image_path, content, img_buket_path, span_type, summary_override=''):+ body_parts = []+ media_path = _build_media_path(img_buket_path, image_path)+ if media_path:+ body_parts.append(f"")++ details_block = _build_visual_details_block(+ content,+ span_type,+ summary_override=summary_override,+ )+ if details_block:+ body_parts.append(details_block)++ return '\n\n'.join(body_parts)+++def _build_visual_markdown(para_block, img_buket_path, caption_type, footnote_type, span_type):+ image_path, content = get_body_data(para_block)+ body_markdown = _build_visual_body_markdown(+ image_path,+ content,+ img_buket_path,+ span_type,+ summary_override=para_block.get('sub_type', ''),+ )+ captions = [+ merge_para_with_text(block)+ for block in para_block.get('blocks', [])+ if block['type'] == caption_type+ ]+ footnotes = [+ merge_para_with_text(block)+ for block in para_block.get('blocks', [])+ if block['type'] == footnote_type+ ]++ sections = []+ if footnotes:+ sections.extend(filter(None, captions))+ if body_markdown:+ sections.append(body_markdown)+ sections.extend(filter(None, footnotes))+ else:+ if body_markdown:+ sections.append(body_markdown)+ sections.extend(filter(None, captions))++ return ' \n'.join(section for section in sections if section)++def merge_para_with_text(para_block, formula_enable=True, img_buket_path=''):block_text = ''for line in para_block['lines']:@@ -143,34 +227,13 @@ def mk_blocks_to_markdown(para_blocks, make_mode, formula_enable, table_enable,if make_mode == MakeMode.NLP_MD:continueelif make_mode == MakeMode.MM_MD:- # 检测是否存在图片脚注- has_image_footnote = any(block['type'] == BlockType.IMAGE_FOOTNOTE for block in para_block['blocks'])- # 如果存在图片脚注,则将图片脚注拼接到图片正文后面- if has_image_footnote:- for block in para_block['blocks']: # 1st.拼image_caption- if block['type'] == BlockType.IMAGE_CAPTION:- para_text += merge_para_with_text(block) + ' \n'- for block in para_block['blocks']: # 2nd.拼image_body- if block['type'] == BlockType.IMAGE_BODY:- for line in block['lines']:- for span in line['spans']:- if span['type'] == ContentType.IMAGE:- if span.get('image_path', ''):- para_text += f""- for block in para_block['blocks']: # 3rd.拼image_footnote- if block['type'] == BlockType.IMAGE_FOOTNOTE:- para_text += ' \n' + merge_para_with_text(block)- else:- for block in para_block['blocks']: # 1st.拼image_body- if block['type'] == BlockType.IMAGE_BODY:- for line in block['lines']:- for span in line['spans']:- if span['type'] == ContentType.IMAGE:- if span.get('image_path', ''):- para_text += f""- for block in para_block['blocks']: # 2nd.拼image_caption- if block['type'] == BlockType.IMAGE_CAPTION:- para_text += ' \n' + merge_para_with_text(block)+ para_text = _build_visual_markdown(+ para_block,+ img_buket_path,+ BlockType.IMAGE_CAPTION,+ BlockType.IMAGE_FOOTNOTE,+ ContentType.IMAGE,+ )elif para_type == BlockType.TABLE:if make_mode == MakeMode.NLP_MD:@@ -196,6 +259,17 @@ def mk_blocks_to_markdown(para_blocks, make_mode, formula_enable, table_enable,for block in para_block['blocks']: # 3rd.拼table_footnoteif block['type'] == BlockType.TABLE_FOOTNOTE:para_text += '\n' + merge_para_with_text(block) + ' '+ elif para_type == BlockType.CHART:+ if make_mode == MakeMode.NLP_MD:+ continue+ elif make_mode == MakeMode.MM_MD:+ para_text = _build_visual_markdown(+ para_block,+ img_buket_path,+ BlockType.CHART_CAPTION,+ BlockType.CHART_FOOTNOTE,+ ContentType.CHART,+ )elif para_type == BlockType.CODE:sub_type = para_block["sub_type"]for block in para_block['blocks']: # 1st.拼code_caption@@ -261,13 +335,10 @@ def make_blocks_to_content_list(para_block, img_buket_path, page_idx, page_size)}elif para_type == BlockType.IMAGE:para_content = {'type': ContentType.IMAGE, 'img_path': '', BlockType.IMAGE_CAPTION: [], BlockType.IMAGE_FOOTNOTE: []}+ image_path, _ = get_body_data(para_block)+ para_content['img_path'] = _build_media_path(img_buket_path, image_path)+ _apply_visual_sub_type(para_content, para_block)for block in para_block['blocks']:- if block['type'] == BlockType.IMAGE_BODY:- for line in block['lines']:- for span in line['spans']:- if span['type'] == ContentType.IMAGE:- if span.get('image_path', ''):- para_content['img_path'] = f"{img_buket_path}/{span['image_path']}"if block['type'] == BlockType.IMAGE_CAPTION:para_content[BlockType.IMAGE_CAPTION].append(merge_para_with_text(block))if block['type'] == BlockType.IMAGE_FOOTNOTE:@@ -293,6 +364,21 @@ def make_blocks_to_content_list(para_block, img_buket_path, page_idx, page_size)para_content[BlockType.TABLE_CAPTION].append(merge_para_with_text(block))if block['type'] == BlockType.TABLE_FOOTNOTE:para_content[BlockType.TABLE_FOOTNOTE].append(merge_para_with_text(block))+ elif para_type == BlockType.CHART:+ image_path, chart_content = get_body_data(para_block)+ para_content = {+ 'type': ContentType.CHART,+ 'img_path': _build_media_path(img_buket_path, image_path),+ 'content': chart_content if chart_content else '',+ BlockType.CHART_CAPTION: [],+ BlockType.CHART_FOOTNOTE: [],+ }+ _apply_visual_sub_type(para_content, para_block)+ for block in para_block['blocks']:+ if block['type'] == BlockType.CHART_CAPTION:+ para_content[BlockType.CHART_CAPTION].append(merge_para_with_text(block))+ if block['type'] == BlockType.CHART_FOOTNOTE:+ para_content[BlockType.CHART_FOOTNOTE].append(merge_para_with_text(block))elif para_type == BlockType.CODE:para_content = {'type': BlockType.CODE, 'sub_type': para_block["sub_type"], BlockType.CODE_CAPTION: []}for block in para_block['blocks']:@@ -391,7 +477,7 @@ def make_blocks_to_content_list_v2(para_block, img_buket_path, page_size):image_footnote = []image_path, _ = get_body_data(para_block)image_source = {- 'path': f"{img_buket_path}/{image_path}",+ 'path': _build_media_path(img_buket_path, image_path),}for block in para_block['blocks']:if block['type'] == BlockType.IMAGE_CAPTION:@@ -406,6 +492,7 @@ def make_blocks_to_content_list_v2(para_block, img_buket_path, page_size):'image_footnote': image_footnote,}}+ _apply_visual_sub_type(para_content, para_block)elif para_type == BlockType.TABLE:table_caption = []table_footnote = []@@ -443,6 +530,27 @@ def make_blocks_to_content_list_v2(para_block, img_buket_path, page_size):'table_nest_level': table_nest_level,}}+ elif para_type == BlockType.CHART:+ chart_caption = []+ chart_footnote = []+ image_path, chart_content = get_body_data(para_block)+ for block in para_block['blocks']:+ if block['type'] == BlockType.CHART_CAPTION:+ chart_caption.extend(merge_para_with_text_v2(block))+ if block['type'] == BlockType.CHART_FOOTNOTE:+ chart_footnote.extend(merge_para_with_text_v2(block))+ para_content = {+ 'type': ContentTypeV2.CHART,+ 'content': {+ 'image_source': {+ 'path': _build_media_path(img_buket_path, image_path),+ },+ 'content': chart_content if chart_content else '',+ 'chart_caption': chart_caption,+ 'chart_footnote': chart_footnote,+ }+ }+ _apply_visual_sub_type(para_content, para_block)elif para_type == BlockType.CODE:code_caption = []code_content = []@@ -529,10 +637,12 @@ def make_blocks_to_content_list_v2(para_block, img_buket_path, page_size):def get_body_data(para_block):"""- Extract image_path and html from para_block+ Extract image_path and body content from para_blockReturns:- - For IMAGE/INTERLINE_EQUATION: (image_path, '')+ - For IMAGE: (image_path, content)- For TABLE: (image_path, html)+ - For CHART: (image_path, content)+ - For INTERLINE_EQUATION: (image_path, content)- Default: ('', '')"""@@ -542,8 +652,10 @@ def get_body_data(para_block):span_type = span.get('type')if span_type == ContentType.TABLE:return span.get('image_path', ''), span.get('html', '')+ elif span_type == ContentType.CHART:+ return span.get('image_path', ''), span.get('content', '')elif span_type == ContentType.IMAGE:- return span.get('image_path', ''), ''+ return span.get('image_path', ''), span.get('content', '')elif span_type == ContentType.INTERLINE_EQUATION:return span.get('image_path', ''), span.get('content', '')elif span_type == ContentType.TEXT:@@ -554,7 +666,7 @@ def get_body_data(para_block):if 'blocks' in para_block:for block in para_block['blocks']:block_type = block.get('type')- if block_type in [BlockType.IMAGE_BODY, BlockType.TABLE_BODY, BlockType.CODE_BODY]:+ if block_type in [BlockType.IMAGE_BODY, BlockType.TABLE_BODY, BlockType.CHART_BODY, BlockType.CODE_BODY]:result = get_data_from_spans(block.get('lines', []))if result != ('', ''):return result