之前尝试过使用 WPS 和 LibreOffice 批量转换 DOCX 为 PDF,但发现导出的 PDF 存在页边距变大、上下留白增加、分页错乱等问题。
如果 Mac 已安装 Microsoft Word,可以直接通过 AppleScript 调用 Word 导出 PDF,生成效果与 Word 手动导出基本一致。
osascript <<'EOF'tell application "Microsoft Word"open POSIX file "/Users/mac/Downloads/1/1.docx"set d to active documentsave as d file name "/Users/mac/Downloads/1/1.pdf" file format format PDFclose d saving noend tellEOF
例如需要转换:
/Users/mac/Downloads/1
目录下所有 .docx 文件:
DIR="/Users/mac/Downloads/1"for f in "$DIR"/*.docx; do[ -f "$f" ] || continuepdf="${f%.docx}.pdf"osascript <<EOFtell application "Microsoft Word"open POSIX file "$f"set d to active documentsave as d file name "$pdf" file format format PDFclose d saving noend tellEOFecho "Converted: $(basename "$f")"done
转换后:
1.docx->1.pdf2.docx->2.pdf3.docx->3.pdf
PDF 会保存在原文件所在目录。
相比 WPS、LibreOffice 转换,直接调用 Microsoft Word 导出的 PDF 更能保持原始排版。