feat: 统一文档解析器项目 - 迁移 lyxy-reader-office 和 lyxy-reader-html

## 功能特性

- 建立统一的项目结构,包含 core/、readers/、utils/、tests/ 模块
- 迁移 lyxy-reader-office 的所有解析器(docx、xlsx、pptx、pdf)
- 迁移 lyxy-reader-html 的所有解析器(html、url 下载)
- 统一 CLI 入口为 lyxy_document_reader.py
- 统一 Markdown 后处理逻辑
- 按文件类型组织 readers,每个解析器独立文件
- 依赖分组按文件类型细分(docx、xlsx、pptx、pdf、html、http)
- PDF OCR 解析器优先,无参数控制
- 使用 logging 模块替代简单 print
- 设计完整的单元测试结构
- 重写项目文档

## 新增目录/文件

- core/ - 核心模块(异常体系、Markdown 工具、解析调度器)
- readers/ - 格式阅读器(base.py + docx/xlsx/pptx/pdf/html)
- utils/ - 工具函数(文件类型检测)
- tests/ - 测试(conftest.py + test_core/ + test_readers/ + test_utils/)
- lyxy_document_reader.py - 统一 CLI 入口

## 依赖分组

- docx - DOCX 文档解析支持
- xlsx - XLSX 文档解析支持
- pptx - PPTX 文档解析支持
- pdf - PDF 文档解析支持(含 OCR)
- html - HTML/URL 解析支持
- http - HTTP/URL 下载支持
- office - Office 格式组合(docx/xlsx/pptx/pdf)
- web - Web 格式组合(html/http)
- full - 完整功能
- dev - 开发依赖
This commit is contained in:
2026-03-08 13:46:37 +08:00
parent eb8973495e
commit 833018d451
66 changed files with 4054 additions and 0 deletions

89
readers/html/__init__.py Normal file
View File

@@ -0,0 +1,89 @@
"""HTML/URL 文件阅读器,支持多种解析方法。"""
import os
from typing import List, Optional, Tuple
from readers.base import BaseReader
from utils import is_html_file, is_url
from . import cleaner
from . import downloader
from . import trafilatura
from . import domscribe
from . import markitdown
from . import html2text
PARSERS = [
("trafilatura", lambda c, t: trafilatura.parse(c)),
("domscribe", lambda c, t: domscribe.parse(c)),
("MarkItDown", lambda c, t: markitdown.parse(c, t)),
("html2text", lambda c, t: html2text.parse(c)),
]
class HtmlReader(BaseReader):
"""HTML/URL 文件阅读器"""
@property
def supported_extensions(self) -> List[str]:
return [".html", ".htm"]
def supports(self, file_path: str) -> bool:
return is_url(file_path) or is_html_file(file_path)
def download_and_parse(self, url: str) -> Tuple[Optional[str], List[str]]:
"""下载 URL 并解析"""
all_failures = []
# 下载 HTML
html_content, download_failures = downloader.download_html(url)
all_failures.extend(download_failures)
if html_content is None:
return None, all_failures
# 清理 HTML
html_content = cleaner.clean_html_content(html_content)
# 解析 HTML
content, parse_failures = self._parse_html_content(html_content, None)
all_failures.extend(parse_failures)
return content, all_failures
def _parse_html_content(self, html_content: str, temp_file_path: Optional[str]) -> Tuple[Optional[str], List[str]]:
"""解析 HTML 内容"""
failures = []
content = None
for parser_name, parser_func in PARSERS:
content, error = parser_func(html_content, temp_file_path)
if content is not None:
return content, failures
else:
failures.append(f"- {parser_name}: {error}")
return None, failures
def parse(self, file_path: str) -> Tuple[Optional[str], List[str]]:
all_failures = []
if is_url(file_path):
return self.download_and_parse(file_path)
# 读取 HTML 文件
try:
with open(file_path, 'r', encoding='utf-8') as f:
html_content = f.read()
except Exception as e:
return None, [f"- 读取文件失败: {str(e)}"]
# 清理 HTML
html_content = cleaner.clean_html_content(html_content)
# 解析 HTML
content, parse_failures = self._parse_html_content(html_content, file_path)
all_failures.extend(parse_failures)
return content, all_failures