温馨提示×

centos liboffice如何转换

小樊
90
2026-09-06 10:59:47
栏目: 智能运维

在 CentOS 上,LibreOffice 可以在无界面(headless)模式下进行文档格式转换,常用于把 Word、Excel、PPT 等转成 PDF 或其他格式。

下面按常见场景说明。


一、安装 LibreOffice(CentOS)

CentOS 7

yum install -y libreoffice-headless libreoffice-writer libreoffice-calc libreoffice-impress

CentOS 8 / Stream

dnf install -y libreoffice-headless libreoffice-writer libreoffice-calc libreoffice-impress

安装完成后验证:

libreoffice --version
# 或
soffice --version

二、基本转换命令(命令行)

1️⃣ 转换为 PDF(最常用)

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

或指定输出目录:

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

2️⃣ Word → PDF

libreoffice --headless --convert-to pdf:.writer_pdf_Export doc.docx

3️⃣ Excel → PDF

libreoffice --headless --convert-to pdf xls.xlsx

4️⃣ PPT → PDF

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

三、转换为其他格式

Word → HTML

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

Word → TXT

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

Excel → CSV

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

四、批量转换

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

五、常见问题 & 解决方法

❌ 1. 转换无反应 / 卡住

通常是 用户目录冲突(root 下容易出问题)

✅ 解决:

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

或统一用普通用户执行。


❌ 2. 中文乱码

安装中文字体:

yum install -y wqy-zenhei-fonts

❌ 3. 使用 soffice 而不是 libreoffice

有些版本只有:

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

六、结合脚本(示例)

#!/bin/bash
INPUT=$1
OUTPUT_DIR=/data/pdf

libreoffice --headless \
  -env:UserInstallation=file:///tmp/lo_$RANDOM \
  --convert-to pdf --outdir $OUTPUT_DIR $INPUT

如果你有 具体转换需求(如 docx→pdf、批量、Java/PHP 调用、Docker 环境),可以告诉我,我给你更精确的方案。

0 踩