LibOffice在Linux上的实用技巧
通过系统包管理器(如CentOS的yum)可直接安装最新稳定版,命令示例:
sudo yum install libreoffice
安装完成后,可通过libreoffice --version验证是否成功。
若打开文档出现乱码或字体缺失,需安装中文字体(如思源黑体、宋体):
.ttf格式),复制到/usr/share/fonts/目录;sudo mkfontscale && sudo mkfontdir && sudo fc-cache -fv更新字体缓存,使字体生效。libreoffice --writer(文字处理)、libreoffice --calc(电子表格)、libreoffice --draw(绘图);libreoffice --writer --nologo(跳过启动动画);libreoffice --writer --minimized(不显示窗口直接进入后台)。libreoffice --headless --convert-to pdf input.docx --outdir ./outputfind命令批量处理当前目录及子目录的Word文档:find . -name "*.docx" | while read file; do libreoffice --headless --convert-to pdf "$file"; donelibreoffice --headless -p example.odt(发送到默认打印机)。libreoffice --writer -n template.odt(将文档作为模板打开,保存时不覆盖原文件);libreoffice --view important.docx(防止意外修改重要文档)。Ctrl+N:新建文档;Ctrl+O:打开文档;Ctrl+S:保存文档;Ctrl+C/V/X:复制/粘贴/剪切;Ctrl+Z/Y:撤销/重做;Ctrl+F/H:查找/替换。Ctrl+A:全选内容;Ctrl+Home:跳转到文档开头(A1单元格/第一页);Ctrl+End:跳转到文档末尾;Shift+箭头键:扩展选区;Ctrl+Shift+箭头键:快速选中整行/列。Ctrl+B/I/U:加粗/斜体/下划线;Ctrl+1:打开“设置单元格格式”(Calc)或“段落样式”(Writer);Ctrl+5:设置1.5倍行距(Writer);Ctrl+Shift+%:应用百分比格式(Calc)。Unoconv是基于LibOffice的命令行转换工具,支持批量处理:
sudo yum install unoconv;unoconv -f pdf example.docx(将Word转为PDF)。通过Documents4j库实现Java应用与LibOffice集成,例如将Word转为PDF:
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
public class PdfUtil {
public static String convertWordToPdf(String filePath) {
try {
IConverter converter = LocalConverter.builder()
.baseFolder(new File("/opt/libreoffice/program")) // LibOffice安装路径
.workerPool(5, 5, 5)
.build();
return converter.convert(new File(filePath))
.as(DocumentType.PDF)
.toFile(new File("/output/result.pdf"))
.get().toString();
} catch (Exception e) {
e.printStackTrace();
return "转换失败";
}
}
}
```。
### **五、兼容性与优化建议**
#### 1. 更新至最新版本
通过Flatpak或Snap安装可自动更新,避免版本过时导致的兼容问题:
- Flatpak:`flatpak install flathub org.libreoffice.LibreOffice`;
- Snap:`sudo snap install libreoffice --classic`。
#### 2. 检查文档格式
若遇到无法打开的文档,尝试用LibOffice的“另存为”功能转换为兼容格式(如`.odt`、`.xlsx`),或使用在线转换工具预处理。