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 Excel to HTML conversion by extracting non-empty rows and rendering compact tables
myhloli10f2b9b9cf35
diff --git a/mineru/backend/utils/office_chart.py b/mineru/backend/utils/office_chart.pyindex 3c799532..ef328f43 100644--- a/mineru/backend/utils/office_chart.py+++ b/mineru/backend/utils/office_chart.py@@ -1,13 +1,11 @@# Copyright (c) Opendatalab. All rights reserved.import math-import refrom dataclasses import dataclass, fieldfrom datetime import date, datetime, timefrom html import escapefrom io import BytesIOfrom typing import Any, Final-import pandas as pdfrom lxml import etreefrom openpyxl import load_workbookfrom openpyxl.utils.cell import range_to_tuple@@ -66,32 +64,98 @@ class ChartSpec:series: list[SeriesSpec] = field(default_factory=list)-def minify_html(html: str) -> str:- """Strip formatting whitespace from HTML while preserving content."""- if not html:- return html-- html = re.sub(r'>\s+<', '><', html)- html = re.sub(r'\n\s*', '', html)- return html--def html_table_from_excel_bytes(excel_bytes: bytes) -> str:"""Convert the first non-empty worksheet in an embedded workbook to HTML."""if not excel_bytes:return ""- worksheets = pd.read_excel(BytesIO(excel_bytes), sheet_name=None)- for dataframe in worksheets.values():- if dataframe is None:- continue- if dataframe.empty and len(dataframe.columns) == 0:- continue- return minify_html(dataframe.to_html(index=False, header=True))+ try:+ workbook = load_workbook(+ filename=BytesIO(excel_bytes),+ data_only=True,+ read_only=True,+ )+ except Exception:+ return ""++ try:+ for worksheet in workbook.worksheets:+ rows = _extract_non_empty_worksheet_rows(worksheet)+ if rows:+ return _render_embedded_workbook_table(rows)+ finally:+ workbook.close()return ""+def _extract_non_empty_worksheet_rows(worksheet) -> list[list[str]]:+ """提取工作表中首尾有内容的行,避免空 sheet 或尾部空列撑大兜底表格。"""+ raw_rows: list[list[str]] = []+ for row in worksheet.iter_rows(values_only=True):+ stringified_row = [_stringify_cell_value(value) for value in row]+ raw_rows.append(_trim_trailing_empty_values(stringified_row))++ first_row_idx = _find_first_non_empty_row(raw_rows)+ if first_row_idx is None:+ return []++ last_row_idx = _find_last_non_empty_row(raw_rows)+ if last_row_idx is None:+ return []++ last_row_exclusive = last_row_idx + 1+ rows = raw_rows[first_row_idx:last_row_exclusive]+ width = max(len(row) for row in rows)+ return [row + [""] * (width - len(row)) for row in rows]+++def _trim_trailing_empty_values(values: list[str]) -> list[str]:+ """移除行尾空值,保留中间空单元格的位置。"""+ end = len(values)+ while end > 0 and values[end - 1] == "":+ end -= 1+ return values[:end]+++def _find_first_non_empty_row(rows: list[list[str]]) -> int | None:+ """返回第一行非空行的索引,未找到时返回 None。"""+ for idx, row in enumerate(rows):+ if any(value != "" for value in row):+ return idx+ return None+++def _find_last_non_empty_row(rows: list[list[str]]) -> int | None:+ """返回最后一行非空行的索引。"""+ for idx in range(len(rows) - 1, -1, -1):+ if any(value != "" for value in rows[idx]):+ return idx+ return None+++def _render_embedded_workbook_table(rows: list[list[str]]) -> str:+ """将嵌入 workbook 的二维数据渲染为紧凑 HTML 表格,首行作为表头。"""+ if not rows:+ return ""++ headers = rows[0]+ data_rows = rows[1:]+ html_parts = ["<table><thead><tr>"]+ for header in headers:+ html_parts.append(f"<th>{escape(header)}</th>")+ html_parts.append("</tr></thead><tbody>")++ for row in data_rows:+ html_parts.append("<tr>")+ for value in row:+ html_parts.append(f"<td>{escape(value)}</td>")+ html_parts.append("</tr>")++ html_parts.append("</tbody></table>")+ return "".join(html_parts)++def extract_chart_html_from_ooxml(chart_xml: bytes, workbook_bytes: bytes | None) -> str:spec = parse_chart_spec_from_ooxml(chart_xml)if spec is None or not spec.series:diff --git a/pyproject.toml b/pyproject.tomlindex 5fe9c79d..17edca98 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -59,7 +59,6 @@ dependencies = ["mammoth>=1.11.0,<2","pylatexenc>=2.10,<3","lxml>=4.0.0,<7.0.0",- "pandas>=2.3.3,<3","openpyxl>=3.1.5,<4","fastapi","python-multipart",