feat: 添加 doc/xls/ppt 旧格式文档支持

- 新增 DocReader,支持 markitdown 和 pypandoc-binary 解析器
- 新增 XlsReader,支持 unstructured、markitdown 和 pandas+xlrd 解析器
- 新增 PptReader,支持 markitdown 解析器
- 添加 olefile 依赖用于验证 OLE2 格式
- 更新 config.py 添加 doc/xls/ppt 依赖配置
- 更新 --advice 支持 doc/xls/ppt 格式
- 添加相应的测试用例
- 同步 specs 到主目录
This commit is contained in:
2026-03-10 23:09:13 +08:00
parent e53e64d386
commit cf10458dd6
32 changed files with 756 additions and 5 deletions

View File

@@ -0,0 +1,50 @@
"""XLS 文件阅读器,支持多种解析方法。"""
import os
from typing import List, Optional, Tuple
from readers.base import BaseReader
from utils import is_valid_xls
from . import unstructured
from . import markitdown
from . import pandas
PARSERS = [
("unstructured", unstructured.parse),
("MarkItDown", markitdown.parse),
("pandas+xlrd", pandas.parse),
]
class XlsReader(BaseReader):
"""XLS 文件阅读器"""
def supports(self, file_path: str) -> bool:
return file_path.lower().endswith('.xls')
def parse(self, file_path: str) -> Tuple[Optional[str], List[str]]:
failures = []
# 检查文件是否存在
if not os.path.exists(file_path):
return None, ["文件不存在"]
# 验证文件格式
if not is_valid_xls(file_path):
return None, ["不是有效的 XLS 文件"]
content = None
for parser_name, parser_func in PARSERS:
try:
content, error = parser_func(file_path)
if content is not None:
return content, failures
else:
failures.append(f"- {parser_name}: {error}")
except Exception as e:
failures.append(f"- {parser_name}: [意外异常] {type(e).__name__}: {str(e)}")
return None, failures

View File

@@ -0,0 +1,10 @@
"""使用 MarkItDown 库解析 XLS 文件"""
from typing import Optional, Tuple
from readers._utils import parse_via_markitdown
def parse(file_path: str) -> Tuple[Optional[str], Optional[str]]:
"""使用 MarkItDown 库解析 XLS 文件"""
return parse_via_markitdown(file_path)

View File

@@ -0,0 +1,41 @@
"""使用 pandas+xlrd 库解析 XLS 文件"""
from typing import Optional, Tuple
def parse(file_path: str) -> Tuple[Optional[str], Optional[str]]:
"""使用 pandas+xlrd 库解析 XLS 文件"""
try:
import pandas as pd
from tabulate import tabulate
except ImportError as e:
if "pandas" in str(e):
missing_lib = "pandas"
elif "xlrd" in str(e):
missing_lib = "xlrd"
else:
missing_lib = "tabulate"
return None, f"{missing_lib} 库未安装"
try:
sheets = pd.read_excel(file_path, sheet_name=None, engine="xlrd")
markdown_parts = []
for sheet_name, df in sheets.items():
if len(df) == 0:
markdown_parts.append(f"## {sheet_name}\n\n*工作表为空*")
continue
table_md = tabulate(
df, headers="keys", tablefmt="pipe", showindex=True, missingval=""
)
markdown_parts.append(f"## {sheet_name}\n\n{table_md}")
if not markdown_parts:
return None, "Excel 文件为空"
markdown_content = "# Excel数据转换结果\n\n" + "\n\n".join(markdown_parts)
return markdown_content, None
except Exception as e:
return None, f"pandas 解析失败: {str(e)}"

View File

@@ -0,0 +1,22 @@
"""使用 unstructured 库解析 XLS 文件"""
from typing import Optional, Tuple
from readers._utils import convert_unstructured_to_markdown
def parse(file_path: str) -> Tuple[Optional[str], Optional[str]]:
"""使用 unstructured 库解析 XLS 文件"""
try:
from unstructured.partition.xlsx import partition_xlsx
except ImportError:
return None, "unstructured 库未安装"
try:
elements = partition_xlsx(filename=file_path, infer_table_structure=True)
content = convert_unstructured_to_markdown(elements)
if not content.strip():
return None, "文档为空"
return content, None
except Exception as e:
return None, f"unstructured 解析失败: {str(e)}"