LibreOffice
开源办公套件 LibreOffice
LibreOffice 是由 The Document Foundation 开发的开源办公套件,起源于 OpenOffice.org 的分支项目。它遵循 GPL/LGPL 开源协议,完全免费且允许用户自由修改和分发代码。支持 Windows、macOS、Linux 三大操作系统,提供一致的跨平台体验。
LibreOffice 六大核心组件
Writer(文字处理)
功能对标 Microsoft Word,支持长文档编辑、样式管理、目录生成等。
兼容格式:.odt(默认)、.docx、.doc、.rtf、.txt 等。
特色:内置 PDF 导出、宏脚本支持、导航窗格快速定位章节。
Calc(电子表格)
类似 Microsoft Excel,支持百万行数据处理、复杂公式(如 VLOOKUP、SUMIF)及数据透视表。
兼容格式:.ods、.xlsx、.csv。
优势:支持 Python 脚本扩展数据分析能力。
Impress(演示文稿)
对标 PowerPoint,提供动画效果、幻灯片模板及多媒体嵌入功能。
兼容格式:.odp、.pptx、.ppt。
创新:支持 SVG 矢量图直接编辑,提升视觉设计灵活性。
Draw(矢量绘图)
用于流程图、技术图纸、海报设计,支持图层管理和 PDF 注释。
兼容格式:.odg、.svg、.vsd(Visio 格式)。
Base(数据库管理)
类似 Microsoft Access,支持创建表单、查询及报表,可连接 MySQL、PostgreSQL 等外部数据库。
Math(公式编辑器)
专为学术设计,支持 LaTeX 语法,可嵌入其他文档中。
安装 LibreOffice
Mac brew 安装 LibreOffice
macOS brew 安装 LibreOffice
brew install --cask libreoffice
会同时安装 soffice 命令和 LibreOffice 图形界面软件
CentOS 安装 LibreOffice
sudo yum remove libreoffice-* # 卸载旧版本(可选)
sudo yum install libreoffice
soffice 命令
soffice (1) - Linux Manuals
https://www.systutorials.com/docs/linux/man/1-soffice/
soffice
是 LibreOffice 的命令行接口,支持无界面操作(–headless),适用于批量处理、服务器部署和自动化任务。
其主要功能包括:
- 文档格式转换:批量转换 DOC/DOCX、ODT、PDF、HTML 等格式
- 打印控制:直接打印文档或生成虚拟打印文件(如 PDF)
- 宏脚本执行:调用 Python/Basic 宏实现自动化操作(如重命名工作表、数据填充)
- 无界面模式操作:服务器环境下处理文档,节省资源
- 文档元数据管理:查看或修改文档属性(需配合 unoconv 工具)
选项:--headless
无界面模式(但我在 Mac 上命令行测试还是会自动弹出一下 LibreOffice 图形界面又关闭)--convert-to docx
批量转换文件,指定目标格式为 DOCX--outdir
指定输出目录(可选),默认输出到当前目录
doc 转 docx
将 aaa.doc 转换为 docx 格式,指定输出目录soffice --headless --convert-to docx --outdir /dir/ /path-to/aaa.doc
全部/批量 doc 转 docx
将当前目录下的全部 doc 转换为 docxsoffice --headless --convert-to docx *.doc
docx 转 pdf
soffice --headless --convert-to pdf /path-to/aaa.docx
JODConverter
jodconverter / jodconverter
https://github.com/jodconverter/jodconverter
https://jodconverter.github.io/jodconverter/latest/
JODConverter(Java OpenDocument Converter) 利用 LibreOffice 或 Apache OpenOffice 实现办公文档格式转换。
特性:
- 支持 100+ 文档格式互转(DOC/DOCX/ODT/PDF/HTML 等)
- 提供本地转换(需安装 Office)和远程转换(连接 LibreOffice Online)
- 线程安全、支持文档转换过滤器(如水印、元数据处理)
JODConverter + LibreOffice 实现 DOC 转换为 DOCX
doc2docx 工具类:
@Slf4j
public class OfficeUtils {
private static final LocalOfficeManager OFFICE_MANAGER;
static {
OFFICE_MANAGER = LocalOfficeManager
.builder()
.portNumbers(2002) // 指定端口,JODConverter 通过此端口与 LibreOffice 进程通信
.maxTasksPerProcess(20) // 最大任务数,单个 LibreOffice 进程最多处理 20 个文档后自动重启
.taskExecutionTimeout(30_000L) // 单任务超时 30 秒
.install() // 自动检测并设置 LibreOffice 的安装路径,注意:并不会自动安装 LibreOffice
.build();
try {
OFFICE_MANAGER.start(); // 如果 install() 未找到 LibreOffice 路径,start() 会抛异常 OfficeException
} catch (Exception e) {
log.warn("Failed to start LibreOffice", e);
}
}
public static byte[] doc2docx(byte[] docBytes) {
try (InputStream in = new ByteArrayInputStream(docBytes);
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
DocumentConverter converter = LocalConverter.make(OFFICE_MANAGER);
converter.convert(in)
.as(DefaultDocumentFormatRegistry.DOC)
.to(out)
.as(DefaultDocumentFormatRegistry.DOCX)
.execute();
return out.toByteArray();
} catch (Exception e) {
throw new RuntimeException("DOC to DOCX conversion failed", e);
}
}
}
测试 doc 转 docx
@Test
public void testDoc2Docx() {
String docFile = "/path-to-doc.doc";
String docxFile = docFile + "x";
byte[] docxBytes = OfficeUtils.doc2docx(FileUtil.readBytes(docFile));
FileUtil.writeBytes(docxBytes, docxFile);
}
Apache OpenOffice
Apache OpenOffice
https://www.openoffice.org/
LibreOffice 在几乎所有方面都优于 Apache OpenOffice,推荐优先使用 LibreOffice
上一篇 PyCharm
页面信息
location:
protocol
: host
: hostname
: origin
: pathname
: href
: document:
referrer
: navigator:
platform
: userAgent
: