You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import subprocess
from pathlib import Path
from typing import Optional
def convert_md_to_docx(
input_md: str,
output_docx: str,
reference_template: Optional[str] = None,
update_all_fields: bool = True,
pandoc_path: str = "pandoc"
) -> None:
"""
使用Pandoc将Markdown文件转换为Word文档
:param input_md: 输入的Markdown文件路径
:param output_docx: 输出的Word文件路径
:param reference_template: 参考模板文件路径(可选)
:param update_all_fields: 是否更新所有字段默认True
:param pandoc_path: Pandoc可执行文件路径默认在系统PATH中查找
:raises FileNotFoundError: 输入文件或pandoc不存在时抛出
:raises subprocess.CalledProcessError: 转换失败时抛出
"""
# 验证输入文件存在
input_path = Path(input_md)
if not input_path.exists():
raise FileNotFoundError(f"输入文件不存在: {input_path}")
# 构建命令参数
cmd = [
pandoc_path,
str(input_path),
"-o", str(Path(output_docx)),
]
# 添加模板参数
if reference_template:
cmd.extend(["--reference-doc", str(Path(reference_template))])
# 添加字段更新参数
if update_all_fields:
cmd.append("-M")
cmd.append("update_all_fields=true")
try:
# 执行转换命令
subprocess.run(
cmd,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding='utf-8',
errors='replace'
)
except FileNotFoundError:
raise FileNotFoundError(
f"未找到pandoc请确认已安装或指定正确路径: {pandoc_path}"
)
except subprocess.CalledProcessError as e:
error_msg = f"转换失败: {e.stderr}" if e.stderr else "未知错误"
raise RuntimeError(f"Pandoc转换失败: {error_msg}") from e