Files
lyxy-document/skill/SKILL.md
lanyuanxiaoyao 7eab1dcef1 test: 添加全面的测试套件,覆盖所有 Reader 实现
- 测试数量从 83 个增加到 193 个 (+132%)
- 代码覆盖率从 48% 提升到 69% (+44%)
- 为每种文档格式的所有 Reader 实现创建独立测试
- 添加跨 Reader 的一致性验证测试
- 新增 4 个测试规范 (cli-testing, exception-testing, reader-testing, test-fixtures)
- 更新 README 测试统计信息

测试覆盖:
- DOCX: python-docx, markitdown, docling, native-xml, pypandoc, unstructured
- PDF: pypdf, markitdown, docling, docling-ocr, unstructured, unstructured-ocr
- HTML: html2text, markitdown, trafilatura, domscribe
- PPTX: python-pptx, markitdown, docling, native-xml, unstructured
- XLSX: pandas, markitdown, docling, native-xml, unstructured
- CLI: 所有命令行选项和错误处理

所有 193 个测试通过。
2026-03-08 22:20:21 +08:00

157 lines
6.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
name: lyxy-document-reader
description: 统一文档解析工具 - 将 DOCX、XLSX、PPTX、PDF、HTML/URL 转换为 Markdown。支持全文输出、字数统计、行数统计、标题提取、章节提取、正则搜索。当用户要求"读取/解析/打开文档"、上传 .docx/.xlsx/.pptx/.pdf/.html 文件、或提供 URL 时使用。
license: MIT
metadata:
version: "1.0"
author: lyxy
compatibility: Requires Python 3.11+. 优先使用 lyxy-runner-python skill 执行(自动管理依赖)。回退到主机 Python 时需手动安装依赖DOCX(docling unstructured markitdown pypandoc-binary python-docx markdownify chardet) / XLSX(docling unstructured markitdown pandas tabulate chardet) / PPTX(docling unstructured markitdown python-pptx markdownify chardet) / PDF(docling unstructured unstructured-paddleocr markitdown pypdf markdownify chardet) / HTML(trafilatura domscribe markitdown html2text beautifulsoup4 httpx chardet) / HTTP增强(pyppeteer selenium)
---
# 统一文档解析 Skill
将 DOCX、XLSX、PPTX、PDF、HTML 文件或 URL 网页内容解析为 Markdown 格式,支持多种查询模式。
## Purpose
**统一入口**:使用 `scripts/lyxy_document_reader.py` 作为统一的命令行入口,自动识别文件类型并执行解析。
**双路径执行**:此 skill 必须优先使用 **lyxy-runner-python skill** 执行脚本,该 skill 会自动管理 uv 隔离环境和依赖。当 lyxy-runner-python 不可用时,回退到主机 Python 环境执行。
**支持的文档类型**
- **DOCX**Word 文档
- **XLSX**Excel 表格
- **PPTX**PowerPoint 演示文稿
- **PDF**PDF 文档(支持 OCR
- **HTML/URL**HTML 文件或网页地址
## When to Use
任何需要读取或解析 Office 文档、PDF、HTML 文件、URL 网页内容的任务都应使用此 skill。
### 典型场景
- **文档转换**:将各类文档转换为可读的 Markdown 文本
- **文档元数据**:获取文档的字数、行数等信息
- **标题分析**:提取文档的标题结构
- **章节提取**:提取特定章节的内容
- **内容搜索**:在文档中搜索关键词或正则模式
### 触发词
- **中文**"读取/解析/打开 文档/Word/Excel/PPT/PDF/网页"
- **英文**"read/parse/extract document/docx/xlsx/pptx/pdf/html"
- **文件扩展名**`.docx``.xlsx``.pptx``.pdf``.html``.htm`
- **URL 模式**`http://``https://`
## Quick Reference
| 参数 | 说明 |
|------|------|
| (无参数) | 输出完整 Markdown 内容 |
| `-c` / `--count` | 字数统计 |
| `-l` / `--lines` | 行数统计 |
| `-t` / `--titles` | 提取所有标题1-6级 |
| `-tc <name>` | 提取指定标题的章节内容 |
| `-s <pattern>` | 正则表达式搜索 |
| `-n <num>` / `--context <num>` | 与 `-s` 配合指定上下文行数默认2 |
## Workflow
1. **检测执行环境**
- 优先检测 **lyxy-runner-python skill** 是否可用
- 可用 → 使用 uv 隔离环境执行
- 不可用 → 回退到主机 Python 环境
2. **识别文件类型**
- 根据文件扩展名自动选择对应的解析器
- URL 自动识别为 HTML/网页类型
3. **执行解析**
- 按优先级尝试多个解析器,直到成功
- DOCXdocling → unstructured → pypandoc → MarkItDown → python-docx → XML
- XLSXdocling → unstructured → MarkItDown → pandas → XML
- PPTXdocling → unstructured → MarkItDown → python-pptx → XML
- PDFdocling OCR → unstructured OCR → docling → unstructured → MarkItDown → pypdf
- HTMLtrafilatura → domscribe → MarkItDown → html2text
4. **输出结果**
- 返回 Markdown 格式内容或统计信息
### 基本语法
```bash
# 方式 1使用 lyxy-runner-python推荐
# lyxy-runner-python 会自动分析脚本依赖并使用 uv --with 安装
# AI 只需执行:
python scripts/lyxy_document_reader.py <文件路径或URL>
# 方式 2回退到主机 Python需要预先手动安装依赖
# 根据文档类型安装对应依赖后执行:
python scripts/lyxy_document_reader.py <文件路径或URL>
```
### 使用示例
```bash
# 读取 Word 文档
python scripts/lyxy_document_reader.py document.docx
# 解析网页内容
python scripts/lyxy_document_reader.py https://example.com
# 统计字数
python scripts/lyxy_document_reader.py document.docx -c
# 提取所有标题
python scripts/lyxy_document_reader.py document.docx -t
# 提取指定章节
python scripts/lyxy_document_reader.py document.docx -tc "第三章"
# 搜索内容
python scripts/lyxy_document_reader.py document.docx -s "关键词"
# 正则搜索
python scripts/lyxy_document_reader.py document.docx -s "\d{4}-\d{2}-\d{2}"
# 指定上下文行数
python scripts/lyxy_document_reader.py document.docx -s "关键词" -n 5
```
### 主机 Python 环境依赖安装
当 lyxy-runner-python 不可用时,需要根据文档类型手动安装依赖:
```bash
# DOCX 文档
pip install docling unstructured markitdown pypandoc-binary python-docx markdownify chardet
# XLSX 表格
pip install docling unstructured markitdown pandas tabulate chardet
# PPTX 演示文稿
pip install docling unstructured markitdown python-pptx markdownify chardet
# PDF 文档
pip install docling unstructured unstructured-paddleocr markitdown pypdf markdownify chardet
# HTML/URL 网页
pip install trafilatura domscribe markitdown html2text beautifulsoup4 httpx chardet
# 网页(需要 JS 渲染时,额外添加)
pip install pyppeteer selenium
# 安装所有文档类型支持
pip install docling unstructured unstructured-paddleocr markitdown pypandoc-binary python-docx python-pptx pandas tabulate pypdf markdownify trafilatura domscribe html2text beautifulsoup4 httpx pyppeteer selenium chardet
```
## 错误处理
| 错误信息 | 原因 | 解决 |
|---------|------|------|
| 错误: input_path 不能为空 | 未提供输入 | 提供 file_path 或 URL |
| 错误: 不支持的文件类型 | 无对应 reader | 检查文件扩展名 |
| 所有解析方法均失败 | 所有解析器失败 | 检查文件是否损坏 |
| 错误: 无效的正则表达式 | 正则语法错误 | 检查正则语法 |
| 错误: 未找到匹配 | 搜索无结果 | 检查搜索词或正则 |
| ModuleNotFoundError: No module named 'xxx' | 缺少依赖 | 使用 lyxy-runner-python 或 pip 安装对应依赖 |