1
0

加入pandoc解析docx

This commit is contained in:
2026-02-15 21:54:54 +08:00
parent f167aa2111
commit 4324699a3d
3 changed files with 54 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env python3
"""DOCX 文件解析模块,提供种解析方法。"""
"""DOCX 文件解析模块,提供种解析方法。"""
import xml.etree.ElementTree as ET
import zipfile
@@ -8,6 +8,32 @@ from typing import Any, List, Optional, Tuple
from common import build_markdown_table, parse_with_markitdown, safe_open_zip
def parse_docx_with_pypandoc(file_path: str) -> Tuple[Optional[str], Optional[str]]:
"""使用 pypandoc-binary 库解析 DOCX 文件。"""
try:
import pypandoc
except ImportError:
return None, "pypandoc-binary 库未安装"
try:
content = pypandoc.convert_file(
source_file=file_path,
to="md",
format="docx",
outputfile=None,
extra_args=["--wrap=none"],
)
except OSError as exc:
return None, f"pypandoc-binary 缺少 Pandoc 可执行文件: {exc}"
except RuntimeError as exc:
return None, f"pypandoc-binary 解析失败: {exc}"
content = content.strip()
if not content:
return None, "文档为空"
return content, None
def parse_docx_with_markitdown(file_path: str) -> Tuple[Optional[str], Optional[str]]:
"""使用 MarkItDown 库解析 DOCX 文件"""
return parse_with_markitdown(file_path)