Install
openclaw skills install @zd200572/docx2pdf在 Windows 上用 LibreOffice 的 soffice.exe 无头模式,批量把当前目录下的 .doc / .docx 转成 .pdf。不依赖 Microsoft Word 安装,纯命令行,适合一次性批量处理。 无需 Office 许可证,跨 Windows 版本可用。
openclaw skills install @zd200572/docx2pdf不依赖 Microsoft Word,用 LibreOffice 在命令行批量把 .doc / .docx 转 .pdf。
& 'C:\Program Files\LibreOffice\program\soffice.exe' --headless --invisible --convert-to pdf --outdir ./ *.doc *.docx
如果你的 LibreOffice 装在别的位置,把路径换成你的;或者先设环境变量:
$env:SOFFICE = 'D:\Program Files\LibreOffice\program\soffice.exe'
& $env:SOFFICE --headless --invisible --convert-to pdf --outdir ./ *.doc *.docx
| 段 | 作用 |
|---|---|
& '...soffice.exe' | PowerShell 调用带空格路径的可执行文件,必须用 &(调用运算符) |
--headless --invisible | 无 UI、无弹窗;两个都带,缺一个都可能挂起 |
--convert-to pdf | 输出格式;也可写 pdf:writer_pdf_Export 显式指定导出器 |
--outdir ./ | 输出目录,./ = 当前目录;改 output_folder 输出到子目录 |
*.doc *.docx | PowerShell 通配符展开后逐个传给 soffice(soffice 本身不解析通配符) |
# 只转 .docx
& $env:SOFFICE --headless --invisible --convert-to pdf --outdir ./ *.docx
# 输出到子目录
& $env:SOFFICE --headless --invisible --convert-to pdf --outdir ./pdf_out *.doc *.docx
# 递归子目录(PowerShell 展开)
Get-ChildItem -Recurse -Include *.doc,*.docx | ForEach-Object {
& $env:SOFFICE --headless --invisible --convert-to pdf --outdir $_.DirectoryName $_.FullName
}
# 找到 soffice 真实路径(多版本共存时)
Get-ChildItem 'C:\Program Files','D:\Program Files' -Recurse -Filter soffice.exe -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty FullName
'path\soffice.exe' args → PowerShell 把字符串当字符串,不执行。必须 & 'path' args。./ 或 .\* → soffice 把目录当文件,转换失败。要靠 shell 通配符展开具体文件列表。Program Fil、Program\ Fil 这种截断/转义错乱 → 整段用单引号包起来,别自己拆。Get-Process soffice* -ErrorAction SilentlyContinue | Stop-Process -Force
Start-Sleep -Seconds 2
.pdf 已存在会被静默覆盖,源文件不会有事,但旧 PDF 不会提示。Get-ChildItem 找一下真实位置;LibreOffice 32 位默认在 C:\Program Files (x86)\LibreOffice\program\soffice.exe。| 症状 | 原因 | 解决 |
|---|---|---|
| 命令没反应 | 缺 & 调用运算符 | & 'path\to\soffice.exe' args |
| 报 "is not recognized" | 路径错误或没引号 | 整段路径用单引号包起来 |
| 报 "source file could not be loaded" | 传了目录而非具体文件 | 用 *.doc *.docx 展开,别传 ./ |
| 报 "already running" | 上次 soffice.bin 没退 | Stop-Process -Name soffice* -Force |
| 输出 PDF 排版错乱 | Word 用了 LibreOffice 不支持的功能 | 用 Word 自己"另存为 PDF",或换 PDF 导出器参数 |
要保真排版(尤其有修订、SmartArt、嵌入 Excel 表)时,优先用 Word 自己的"另存为 PDF"。
MIT,随便拿去用。