feat: 新增 .doc 格式支持,借助 LibreOffice soffice
- 提取 LibreOffice 解析逻辑为公共工具函数 _utils.parse_via_libreoffice() - 新增 DocReader 独立 Reader,支持 .doc 格式 - 新增 is_valid_doc() 文件验证函数(复用 OLE2 检测) - 新增 doc 格式依赖配置(独立配置) - 新增完整的测试套件,使用静态测试文件 - 更新 README.md 和 SKILL.md,添加 .doc 格式支持说明 - 新增 openspec/specs/doc-reader/spec.md 规范文档 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
46
scripts/readers/doc/__init__.py
Normal file
46
scripts/readers/doc/__init__.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""DOC 文件阅读器,使用 LibreOffice 解析。"""
|
||||
|
||||
import os
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
from readers.base import BaseReader
|
||||
from utils import is_valid_doc
|
||||
|
||||
from . import libreoffice
|
||||
|
||||
|
||||
PARSERS = [
|
||||
("LibreOffice", libreoffice.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
|
||||
9
scripts/readers/doc/libreoffice.py
Normal file
9
scripts/readers/doc/libreoffice.py
Normal file
@@ -0,0 +1,9 @@
|
||||
"""使用 LibreOffice soffice 命令行解析 DOC 文件"""
|
||||
|
||||
from typing import Optional, Tuple
|
||||
from readers._utils import parse_via_libreoffice
|
||||
|
||||
|
||||
def parse(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""使用 LibreOffice soffice 解析 DOC 文件"""
|
||||
return parse_via_libreoffice(file_path)
|
||||
Reference in New Issue
Block a user