温馨提示×

centos liboffice怎么命令行用

小樊
40
2026-09-12 06:12:24
栏目: 智能运维

在 CentOS 上,LibreOffice 可以通过命令行(无界面)完成很多任务,比如转换文档格式、批量处理、打印、导出 PDF等。下面按常见用法说明。


一、确认 LibreOffice 已安装

which libreoffice
# 或
libreoffice --version

如果没有安装:

sudo yum install libreoffice
# 或最小化安装(只转换用)
sudo yum install libreoffice-headless

二、基本命令行格式

libreoffice [选项] 文件

常用命令名也可能是:

  • libreoffice
  • soffice

三、常用操作示例

1️⃣ 把 Word / Excel / PPT 转 PDF

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

指定输出目录:

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

2️⃣ 转换为其他格式

Word → HTML

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

Excel → CSV

libreoffice --headless --convert-to csv test.xlsx

PPT → PDF

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

3️⃣ 批量转换

for f in *.docx; do
  libreoffice --headless --convert-to pdf "$f"
done

4️⃣ 不启动界面直接运行(重要)

服务器/脚本一定要加:

--headless

否则会尝试打开 GUI,导致卡住或报错。


5️⃣ 指定用户配置目录(服务器常用)

避免多用户冲突:

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

6️⃣ 打印文档

libreoffice --headless --print-to-file --outdir /tmp test.docx

或指定打印机:

libreoffice --headless --printer-name=PDF test.docx

四、常见问题

❌ 报错:no suitable windowing environment

✅ 解决:加 --headless

❌ 转换卡住

✅ 一般是:

  • 已有 LibreOffice 在运行
  • 用户配置冲突
pkill libreoffice
rm -rf ~/.config/libreoffice

五、典型服务器用法(推荐)

libreoffice --headless \
  -env:UserInstallation=file:///tmp/lo_$RANDOM \
  --convert-to pdf --outdir /data/pdf /data/doc/test.docx

如果你有具体需求(比如:

  • 转 PDF
  • Word 批量处理
  • 服务器自动转换
  • Java / Python 调用)

可以告诉我,我给你更精确的命令或脚本。

0