- 新增 DocReader,支持 markitdown 和 pypandoc-binary 解析器 - 新增 XlsReader,支持 unstructured、markitdown 和 pandas+xlrd 解析器 - 新增 PptReader,支持 markitdown 解析器 - 添加 olefile 依赖用于验证 OLE2 格式 - 更新 config.py 添加 doc/xls/ppt 依赖配置 - 更新 --advice 支持 doc/xls/ppt 格式 - 添加相应的测试用例 - 同步 specs 到主目录
49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
"""DOC 文件阅读器,支持多种解析方法。"""
|
|
|
|
import os
|
|
from typing import List, Optional, Tuple
|
|
|
|
from readers.base import BaseReader
|
|
from utils import is_valid_doc
|
|
|
|
from . import markitdown
|
|
from . import pypandoc
|
|
|
|
|
|
PARSERS = [
|
|
("MarkItDown", markitdown.parse),
|
|
("pypandoc-binary", pypandoc.parse),
|
|
]
|
|
|
|
|
|
class DocReader(BaseReader):
|
|
"""DOC 文件阅读器"""
|
|
|
|
def supports(self, file_path: str) -> bool:
|
|
return file_path.lower().endswith('.doc')
|
|
|
|
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_doc(file_path):
|
|
return None, ["不是有效的 DOC 文件"]
|
|
|
|
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
|