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:
@@ -5,6 +5,19 @@ import zipfile
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
def _is_valid_ole(file_path: str) -> bool:
|
||||
"""验证 OLE2 格式文件(DOC/XLS/PPT)"""
|
||||
try:
|
||||
import olefile
|
||||
except ImportError:
|
||||
# 如果 olefile 未安装,就不做严格验证
|
||||
return True
|
||||
try:
|
||||
return olefile.isOleFile(file_path)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def _is_valid_ooxml(file_path: str, required_files: List[str]) -> bool:
|
||||
"""验证 OOXML 格式文件(DOCX/PPTX/XLSX)"""
|
||||
try:
|
||||
@@ -35,6 +48,21 @@ def is_valid_xlsx(file_path: str) -> bool:
|
||||
return _is_valid_ooxml(file_path, _XLSX_REQUIRED)
|
||||
|
||||
|
||||
def is_valid_doc(file_path: str) -> bool:
|
||||
"""验证文件是否为有效的 DOC 格式"""
|
||||
return _is_valid_ole(file_path)
|
||||
|
||||
|
||||
def is_valid_xls(file_path: str) -> bool:
|
||||
"""验证文件是否为有效的 XLS 格式"""
|
||||
return _is_valid_ole(file_path)
|
||||
|
||||
|
||||
def is_valid_ppt(file_path: str) -> bool:
|
||||
"""验证文件是否为有效的 PPT 格式"""
|
||||
return _is_valid_ole(file_path)
|
||||
|
||||
|
||||
def is_valid_pdf(file_path: str) -> bool:
|
||||
"""验证文件是否为有效的 PDF 格式"""
|
||||
try:
|
||||
@@ -61,13 +89,18 @@ _FILE_TYPE_VALIDATORS = {
|
||||
".pptx": is_valid_pptx,
|
||||
".xlsx": is_valid_xlsx,
|
||||
".pdf": is_valid_pdf,
|
||||
".doc": is_valid_doc,
|
||||
".xls": is_valid_xls,
|
||||
".ppt": is_valid_ppt,
|
||||
}
|
||||
|
||||
|
||||
def detect_file_type(file_path: str) -> Optional[str]:
|
||||
"""检测文件类型,返回 'docx'、'pptx'、'xlsx' 或 'pdf'"""
|
||||
"""检测文件类型,返回 'docx'、'pptx'、'xlsx'、'pdf'、'doc'、'xls' 或 'ppt'"""
|
||||
ext = os.path.splitext(file_path)[1].lower()
|
||||
validator = _FILE_TYPE_VALIDATORS.get(ext)
|
||||
if validator and validator(file_path):
|
||||
return ext.lstrip(".")
|
||||
return None
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user