Mac 使用 Microsoft Word 批量将 DOCX 转 PDF(保持原排版)

之前尝试过使用 WPS 和 LibreOffice 批量转换 DOCX 为 PDF,但发现导出的 PDF 存在页边距变大、上下留白增加、分页错乱等问题。
如果 Mac 已安装 Microsoft Word,可以直接通过 AppleScript 调用 Word 导出 PDF,生成效果与 Word 手动导出基本一致。
单个文件转换
bash
osascript <<'EOF'
tell application "Microsoft Word"
open POSIX file "/Users/mac/Downloads/1/1.docx"
set d to active document
save as d file name "/Users/mac/Downloads/1/1.pdf" file format format PDF
close d saving no
end tell
EOF
批量转换整个目录
例如需要转换:
text
/Users/mac/Downloads/1
目录下所有 .docx 文件:
bash
DIR="/Users/mac/Downloads/1"
for f in "$DIR"/*.docx; do
[ -f "$f" ] || continue
pdf="${f%.docx}.pdf"
osascript <<EOF
tell application "Microsoft Word"
open POSIX file "$f"
set d to active document
save as d file name "$pdf" file format format PDF
close d saving no
end tell
EOF
echo "Converted: $(basename "$f")"
done
转换后:
text
1.docx -> 1.pdf
2.docx -> 2.pdf
3.docx -> 3.pdf
PDF 会保存在原文件所在目录。
适用场景
- 合同
- 标书
- 论文
- 公司模板
- 对 PDF 排版一致性要求较高的文档
相比 WPS、LibreOffice 转换,直接调用 Microsoft Word 导出的 PDF 更能保持原始排版。