- 创建 bootstrap.py 承载实际 CLI 逻辑 - 重写 lyxy_document_reader.py 为轻量入口,自动检测依赖并启动 - 使用 subprocess.run() 实现跨平台兼容的自启动 - 移除 --advice 参数及相关测试 - 更新文档和规范,简化使用方式
112 lines
3.1 KiB
Python
112 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
||
"""文档解析器实际执行模块,承载业务逻辑。"""
|
||
|
||
import argparse
|
||
import logging
|
||
import os
|
||
import sys
|
||
import warnings
|
||
from pathlib import Path
|
||
|
||
# 将 scripts/ 目录添加到 sys.path,支持从任意位置执行脚本
|
||
scripts_dir = Path(__file__).resolve().parent
|
||
if str(scripts_dir) not in sys.path:
|
||
sys.path.append(str(scripts_dir))
|
||
|
||
# 抑制第三方库的进度条和日志,仅保留解析结果输出
|
||
os.environ["HF_HUB_DISABLE_PROGRESS_BARS"] = "1"
|
||
os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1"
|
||
os.environ["TQDM_DISABLE"] = "1"
|
||
warnings.filterwarnings("ignore")
|
||
|
||
# 配置日志系统,只输出 ERROR 级别
|
||
logging.basicConfig(level=logging.ERROR, format='%(levelname)s: %(message)s')
|
||
|
||
# 设置第三方库日志等级
|
||
logging.getLogger('docling').setLevel(logging.ERROR)
|
||
logging.getLogger('unstructured').setLevel(logging.ERROR)
|
||
|
||
from core import (
|
||
FileDetectionError,
|
||
ReaderNotFoundError,
|
||
output_result,
|
||
parse_input,
|
||
process_content,
|
||
)
|
||
from readers import READERS
|
||
|
||
|
||
def run_normal(args) -> None:
|
||
"""正常执行模式:解析文件并输出结果"""
|
||
# 实例化所有 readers
|
||
readers = [ReaderCls() for ReaderCls in READERS]
|
||
|
||
try:
|
||
content, failures = parse_input(args.input_path, readers)
|
||
except FileDetectionError as e:
|
||
print(f"错误: {e}")
|
||
sys.exit(1)
|
||
except ReaderNotFoundError as e:
|
||
print(f"错误: {e}")
|
||
sys.exit(1)
|
||
|
||
if content is None:
|
||
print("所有解析方法均失败:")
|
||
for failure in failures:
|
||
print(failure)
|
||
sys.exit(1)
|
||
|
||
# 处理内容
|
||
content = process_content(content)
|
||
|
||
# 输出结果
|
||
output_result(content, args)
|
||
|
||
|
||
def main() -> None:
|
||
"""主函数:解析命令行参数并执行"""
|
||
parser = argparse.ArgumentParser(
|
||
description="将 DOCX、XLS、XLSX、PPTX、PDF、HTML 文件或 URL 解析为 Markdown"
|
||
)
|
||
|
||
parser.add_argument("input_path", help="DOCX、XLS、XLSX、PPTX、PDF、HTML 文件或 URL")
|
||
|
||
parser.add_argument(
|
||
"-n",
|
||
"--context",
|
||
type=int,
|
||
default=2,
|
||
help="与 -s 配合使用,指定每个检索结果包含的前后行数(不包含空行)",
|
||
)
|
||
|
||
group = parser.add_mutually_exclusive_group()
|
||
group.add_argument(
|
||
"-c", "--count", action="store_true", help="返回解析后的 markdown 文档的总字数"
|
||
)
|
||
group.add_argument(
|
||
"-l", "--lines", action="store_true", help="返回解析后的 markdown 文档的总行数"
|
||
)
|
||
group.add_argument(
|
||
"-t",
|
||
"--titles",
|
||
action="store_true",
|
||
help="返回解析后的 markdown 文档的标题行(1-6级)",
|
||
)
|
||
group.add_argument(
|
||
"-tc",
|
||
"--title-content",
|
||
help="指定标题名称,输出该标题及其下级内容(不包含#号)",
|
||
)
|
||
group.add_argument(
|
||
"-s",
|
||
"--search",
|
||
help="使用正则表达式搜索文档,返回所有匹配结果(用---分隔)",
|
||
)
|
||
|
||
args = parser.parse_args()
|
||
run_normal(args)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|