feat: 新增 PPT 旧格式支持,重构 LibreOffice 转换工具
- 新增 PPT (旧格式) 解析器 - 重构 _utils.py,提取通用 convert_via_libreoffice 函数 - 更新依赖配置,添加 PPT 相关依赖 - 完善文档,更新 README 和 SKILL.md - 添加 PPT 文件检测函数 - 新增 PPT 解析器测试用例
This commit is contained in:
@@ -8,6 +8,7 @@ from .pptx import PptxReader
|
||||
from .pdf import PdfReader
|
||||
from .html import HtmlReader
|
||||
from .xls import XlsReader
|
||||
from .ppt import PptReader
|
||||
|
||||
READERS = [
|
||||
DocxReader,
|
||||
@@ -17,6 +18,7 @@ READERS = [
|
||||
PdfReader,
|
||||
HtmlReader,
|
||||
XlsReader,
|
||||
PptReader,
|
||||
]
|
||||
|
||||
__all__ = [
|
||||
@@ -28,5 +30,6 @@ __all__ = [
|
||||
"PdfReader",
|
||||
"HtmlReader",
|
||||
"XlsReader",
|
||||
"PptReader",
|
||||
"READERS",
|
||||
]
|
||||
|
||||
@@ -66,6 +66,75 @@ def parse_via_docling(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
return None, f"docling 解析失败: {str(e)}"
|
||||
|
||||
|
||||
def convert_via_libreoffice(
|
||||
input_path: str,
|
||||
target_format: str,
|
||||
output_dir: Path,
|
||||
output_suffix: Optional[str] = None,
|
||||
timeout: int = 60
|
||||
) -> Tuple[Optional[Path], Optional[str]]:
|
||||
"""使用 LibreOffice soffice 命令行转换文件格式。
|
||||
|
||||
Args:
|
||||
input_path: 输入文件路径
|
||||
target_format: 目标格式(如 "md", "pptx")
|
||||
output_dir: 输出目录(调用者负责生命周期管理)
|
||||
output_suffix: 可选,输出文件后缀(不指定则使用 target_format)
|
||||
timeout: 超时时间(秒)
|
||||
|
||||
Returns:
|
||||
(output_path, error_message): 成功时 (Path, None),失败时 (None, error)
|
||||
"""
|
||||
# 检测 soffice 是否在 PATH 中
|
||||
soffice_path = shutil.which("soffice")
|
||||
if not soffice_path:
|
||||
return None, "LibreOffice 未安装"
|
||||
|
||||
input_file = Path(input_path)
|
||||
suffix = output_suffix if output_suffix else target_format
|
||||
expected_output = output_dir / (input_file.stem + "." + suffix)
|
||||
|
||||
# 构建命令
|
||||
cmd = [
|
||||
soffice_path,
|
||||
"--headless",
|
||||
"--convert-to", target_format,
|
||||
"--outdir", str(output_dir),
|
||||
str(input_file)
|
||||
]
|
||||
|
||||
# 执行命令
|
||||
try:
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=timeout
|
||||
)
|
||||
except subprocess.TimeoutExpired:
|
||||
return None, f"LibreOffice 转换超时 ({timeout}秒)"
|
||||
|
||||
# 检查返回码
|
||||
if result.returncode != 0:
|
||||
return None, f"LibreOffice 转换失败 (code: {result.returncode})"
|
||||
|
||||
# 检查输出文件是否存在
|
||||
output_file = None
|
||||
if expected_output.exists():
|
||||
output_file = expected_output
|
||||
else:
|
||||
# Fallback: 遍历目录找任意匹配后缀的文件
|
||||
pattern = "*." + suffix
|
||||
files = list(output_dir.glob(pattern))
|
||||
if files:
|
||||
output_file = files[0]
|
||||
|
||||
if not output_file:
|
||||
return None, "LibreOffice 未生成输出文件"
|
||||
|
||||
return output_file, None
|
||||
|
||||
|
||||
def parse_via_libreoffice(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""使用 LibreOffice soffice 命令行转换文件为 Markdown。
|
||||
|
||||
@@ -77,56 +146,18 @@ def parse_via_libreoffice(file_path: str) -> Tuple[Optional[str], Optional[str]]
|
||||
Returns:
|
||||
(markdown_content, error_message): 成功时 (content, None),失败时 (None, error)
|
||||
"""
|
||||
# 检测 soffice 是否在 PATH 中
|
||||
soffice_path = shutil.which("soffice")
|
||||
if not soffice_path:
|
||||
return None, "LibreOffice 未安装"
|
||||
|
||||
# 创建临时输出目录
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
temp_path = Path(temp_dir)
|
||||
input_path = Path(file_path)
|
||||
expected_output = temp_path / (input_path.stem + ".md")
|
||||
|
||||
# 构建命令
|
||||
cmd = [
|
||||
soffice_path,
|
||||
"--headless",
|
||||
"--convert-to", "md",
|
||||
"--outdir", str(temp_path),
|
||||
str(input_path)
|
||||
]
|
||||
|
||||
# 执行命令,超时 60 秒
|
||||
try:
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=60
|
||||
)
|
||||
except subprocess.TimeoutExpired:
|
||||
return None, "LibreOffice 转换超时 (60秒)"
|
||||
|
||||
# 检查返回码
|
||||
if result.returncode != 0:
|
||||
return None, f"LibreOffice 转换失败 (code: {result.returncode})"
|
||||
|
||||
# 检查输出文件是否存在
|
||||
output_file = None
|
||||
if expected_output.exists():
|
||||
output_file = expected_output
|
||||
else:
|
||||
# Fallback: 遍历目录找任意 .md 文件
|
||||
md_files = list(temp_path.glob("*.md"))
|
||||
if md_files:
|
||||
output_file = md_files[0]
|
||||
|
||||
if not output_file:
|
||||
return None, "LibreOffice 未生成输出文件"
|
||||
output_path, error = convert_via_libreoffice(
|
||||
input_path=file_path,
|
||||
target_format="md",
|
||||
output_dir=Path(temp_dir),
|
||||
timeout=60
|
||||
)
|
||||
if error:
|
||||
return None, error
|
||||
|
||||
# 读取输出内容
|
||||
content = output_file.read_text(encoding="utf-8", errors="replace")
|
||||
content = output_path.read_text(encoding="utf-8", errors="replace")
|
||||
content = content.strip()
|
||||
|
||||
if not content:
|
||||
|
||||
46
scripts/readers/ppt/__init__.py
Normal file
46
scripts/readers/ppt/__init__.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""PPT 文件阅读器,使用 LibreOffice 解析。"""
|
||||
|
||||
import os
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
from readers.base import BaseReader
|
||||
from utils import is_valid_ppt
|
||||
|
||||
from . import libreoffice
|
||||
|
||||
|
||||
PARSERS = [
|
||||
("LibreOffice", libreoffice.parse),
|
||||
]
|
||||
|
||||
|
||||
class PptReader(BaseReader):
|
||||
"""PPT 文件阅读器"""
|
||||
|
||||
def supports(self, file_path: str) -> bool:
|
||||
return file_path.lower().endswith('.ppt')
|
||||
|
||||
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_ppt(file_path):
|
||||
return None, ["不是有效的 PPT 文件"]
|
||||
|
||||
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
|
||||
37
scripts/readers/ppt/libreoffice.py
Normal file
37
scripts/readers/ppt/libreoffice.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""使用 LibreOffice soffice 命令行转换 PPT 为 PPTX 后复用 PptxReader 解析"""
|
||||
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from readers._utils import convert_via_libreoffice
|
||||
from readers.pptx import PptxReader
|
||||
|
||||
|
||||
def parse(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""使用 LibreOffice soffice 解析 PPT 文件
|
||||
|
||||
Args:
|
||||
file_path: PPT 文件路径
|
||||
|
||||
Returns:
|
||||
(markdown_content, error_message): 成功时 (content, None),失败时 (None, error)
|
||||
"""
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
# 将 PPT 转换为 PPTX
|
||||
pptx_path, error = convert_via_libreoffice(
|
||||
input_path=file_path,
|
||||
target_format="pptx",
|
||||
output_dir=Path(temp_dir),
|
||||
timeout=60
|
||||
)
|
||||
if error:
|
||||
return None, error
|
||||
|
||||
# 复用 PptxReader 解析转换后的 PPTX
|
||||
reader = PptxReader()
|
||||
content, failures = reader.parse(str(pptx_path))
|
||||
if content is not None:
|
||||
return content, None
|
||||
else:
|
||||
return None, f"转换成功但 PPTX 解析失败: {failures}"
|
||||
Reference in New Issue
Block a user