|
|
import subprocess
|
|
|
from pathlib import Path
|
|
|
from typing import Optional
|
|
|
from D1_GenerateMarkdown import *
|
|
|
|
|
|
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
|
|
|
)
|
|
|
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
|
|
|
|
|
|
|
|
|
# 使用示例
|
|
|
if __name__ == "__main__":
|
|
|
try:
|
|
|
convert_md_to_docx(
|
|
|
input_md=mdWorkingPath+r"\5.md",
|
|
|
output_docx=r"D:\dsWork\QingLong\AI\output.docx",
|
|
|
reference_template=r"D:\dsWork\QingLong\AI\left-aligned-template.docx"
|
|
|
)
|
|
|
print("转换成功!")
|
|
|
except Exception as e:
|
|
|
print(f"转换失败: {str(e)}") |