Debian系统LibreOffice文档转换实用技巧
在Debian系统上,首先需要通过包管理器安装LibreOffice。打开终端,执行以下命令更新软件包列表并安装:
sudo apt-get update sudo apt-get install libreoffice 安装完成后,可通过libreoffice --version验证是否安装成功。
LibreOffice提供soffice(或libreoffice)命令行工具,支持多种格式转换。常用示例如下:
input.docx转换为PDF并保存到output_directory目录。libreoffice --headless --invisible --convert-to pdf:writer_pdf_Export input.docx --outdir output_directory input.xlsx转换为CSV格式。libreoffice --headless --convert-to csv:xls_csv_Export input.xlsx input.pptx转换为PDF。libreoffice --headless --convert-to pdf:writer_pdf_Export input.pptx 注:--headless表示无图形界面运行(适合服务器/脚本);--invisible使进程完全后台化;--convert-to指定目标格式及导出引擎;--outdir设置输出目录(可选,默认当前目录)。若需转换多个文件(如目录下所有.docx文件),可结合find命令与循环实现:
find /path/to/documents -name "*.docx" | while read file; do libreoffice --headless --convert-to pdf "$file" --outdir /path/to/output done 此命令会遍历/path/to/documents目录及其子目录中的所有.docx文件,并逐一转换为PDF保存到/path/to/output。
若转换后文档出现中文乱码,需安装对应中文字体(如思源黑体、宋体)并复制到系统字体目录:
sudo cp simhei.ttf /usr/share/fonts/truetype/ sudo cp simsun.ttc /usr/share/fonts/truetype/ 复制完成后,更新字体缓存:
sudo fc-cache -fv 重启LibreOffice使字体生效。
对于大型文档(如超过100MB的Excel文件),转换过程可能较慢。建议在后台运行转换任务(添加&符号),避免阻塞终端:
libreoffice --headless --convert-to pdf large_file.docx --outdir output/ & 或通过nohup命令保持进程运行(即使终端关闭):
nohup libreoffice --headless --convert-to pdf large_file.docx --outdir output/ > conversion.log 2>&1 & 日志会保存到conversion.log文件中。
若偏好图形操作,可通过以下步骤手动转换:
.pdf);对于需要集成到CI/CD或Web应用的场景,可使用UnoServer(基于LibreOffice的文件转换服务器)。安装后,通过unoconverter命令快速转换:
# 启动UnoServer(默认端口2002) unoserver start # 转换文档 unoconverter input.docx output.pdf UnoServer支持并发处理,适合批量或高负载场景。