- 创建 scripts/ 目录作为核心代码根目录 - 移动 core/, readers/, utils/ 到 scripts/ 下 - 移动 config.py, lyxy_document_reader.py 到 scripts/ - 移动 encoding_detection.py 到 scripts/utils/ - 更新 pyproject.toml 中的入口点路径和 pytest 配置 - 更新所有内部导入语句为 scripts.* 模块 - 更新 README.md 目录结构说明 - 更新 openspec/config.yaml 添加目录结构说明 - 删除无用的 main.py 此变更使项目结构更清晰,便于区分核心代码与测试、文档等支撑文件。
35 lines
1.2 KiB
Python
35 lines
1.2 KiB
Python
"""使用 unstructured 库解析 PDF 文件(hi_res 策略 + PaddleOCR)"""
|
||
|
||
from typing import Optional, Tuple
|
||
|
||
from scripts.core import _unstructured_elements_to_markdown
|
||
|
||
|
||
def parse(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
||
"""使用 unstructured 库解析 PDF 文件(hi_res 策略 + PaddleOCR)"""
|
||
try:
|
||
from unstructured.partition.pdf import partition_pdf
|
||
except ImportError:
|
||
return None, "unstructured 库未安装"
|
||
|
||
try:
|
||
from unstructured.partition.utils.constants import OCR_AGENT_PADDLE
|
||
except ImportError:
|
||
return None, "unstructured-paddleocr 库未安装"
|
||
|
||
try:
|
||
elements = partition_pdf(
|
||
filename=file_path,
|
||
infer_table_structure=True,
|
||
strategy="hi_res",
|
||
languages=["chi_sim"],
|
||
ocr_agent=OCR_AGENT_PADDLE,
|
||
table_ocr_agent=OCR_AGENT_PADDLE,
|
||
)
|
||
content = _unstructured_elements_to_markdown(elements, trust_titles=True)
|
||
if not content.strip():
|
||
return None, "文档为空"
|
||
return content, None
|
||
except Exception as e:
|
||
return None, f"unstructured OCR 解析失败: {str(e)}"
|