温馨提示×

centos liboffice如何转换文档

小樊
43
2026-07-09 12:15:43
栏目: 智能运维

CentOS 上用 LibreOffice(libreoffice) 转换文档,常见做法是通过 命令行(headless 模式)。下面按常见场景给你说明。


一、安装 LibreOffice(如未安装)

CentOS 7

yum install -y libreoffice

CentOS 8 / Stream

dnf install -y libreoffice

如需中文支持:

dnf install -y libreoffice-langpack-zh-CN

验证:

libreoffice --version

二、常用转换方式(命令行)

推荐方式:使用 --headless 模式(无界面)

1️⃣ Word / Excel / PPT → PDF(最常用)

libreoffice --headless --convert-to pdf test.docx

指定输出目录:

libreoffice --headless --convert-to pdf --outdir /tmp test.docx

2️⃣ 批量转换

libreoffice --headless --convert-to pdf *.docx

3️⃣ 指定文件类型转换

目标格式 命令
docx → doc libreoffice --headless --convert-to doc test.docx
xlsx → csv libreoffice --headless --convert-to csv test.xlsx
pptx → pdf libreoffice --headless --convert-to pdf test.pptx

4️⃣ HTML / TXT 转换

libreoffice --headless --convert-to html test.docx
libreoffice --headless --convert-to txt test.docx

三、常见问题与解决

❌ 1. 中文乱码

原因:缺少中文字体
解决:

dnf install -y wqy-microhei-fonts wqy-zenhei-fonts

或复制 Windows 字体到:

/usr/share/fonts/
fc-cache -fv

❌ 2. 转换失败 / 无输出

尝试指定 soffice 路径:

/usr/bin/soffice --headless --convert-to pdf test.docx

❌ 3. root 用户无法运行

LibreOffice 默认不允许 root 直接运行:

✅ 解决方式:

libreoffice --headless --convert-to pdf test.docx --norestore -env:UserInstallation=file:///tmp/libreoffice-root

四、在脚本 / 服务器中使用(推荐)

#!/bin/bash
libreoffice --headless --norestore --convert-to pdf "$1"

五、替代方案(如 LibreOffice 太重)

工具 适用
pandoc Markdown / docx / html
unoconv 依赖 LibreOffice
docx2pdf(Python) Windows / macOS

六、示例总结

CentOS + LibreOffice 转 PDF 最简命令

libreoffice --headless --convert-to pdf test.docx

如果你有 具体转换需求(例如:

  • Word → PDF
  • Excel → CSV
  • 批量转换
  • 在 Docker / systemd / PHP / Java 调用)

可以直接告诉我,我给你定制命令或脚本。

0