Python3 - Linux 下安装 LibreOffice 以及使用

Python3 - Linux下安装 LibreOffice 以及使用

LibreOffice是OpenOffice.org办公套件衍生版, 同样自由开源,以Mozilla Public License V2.0分发源代码 ,但相比OpenOffice增加了很多特色功能。LibreOffice拥有强大的数据导入和导出功能,能直接导入PDF文档、微软Word(.doc文件)、LotusWord,支持主要的OpenXML格式。软件本身并不局限于Debian和Ubuntu平台,现已支持Windows、Mac和其它Linux发行版等多个系统平台。

1. 下载/安装 LibreOffice

已Centos 7.9 为例, 需要下载rpm包和语言包:下载rpm包并解压
镜像:
https://mirrors.cloud.tencent.com/libreoffice/libreoffice/stable/
# 创建个文件夹office
[harry@iZ8vbbip4bq97z0o60rq3uZ ~]$ cd /usr/local/
[harry@iZ8vbbip4bq97z0o60rq3uZ local]$ mkdir office 
[harry@iZ8vbbip4bq97z0o60rq3uZ local]$ sudo chmod 777 /usr/local/office 

# 下载rpm 腾讯镜像
[harry@iZ8vbbip4bq97z0o60rq3uZ local]$ wget https://mirrors.cloud.tencent.com/libreoffice/libreoffice/stable/7.3.5/rpm/x86_64/LibreOffice_7.3.5_Linux_x86-64_rpm.tar.gz

# 下载语言包
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ wget https://mirrors.cloud.tencent.com/libreoffice/libreoffice/stable/7.3.5/rpm/x86_64/LibreOffice_7.3.5_Linux_x86-64_rpm_langpack_zh-CN.tar.gz

# 查看一下
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ ls
LibreOffice_7.3.5_Linux_x86-64_rpm_langpack_zh-CN.tar.gz  LibreOffice_7.3.5_Linux_x86-64_rpm.tar.gz

# 解压缩
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ tar -xvf LibreOffice_7.3.5_Linux_x86-64_rpm.tar.gz 
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ tar -xvf LibreOffice_7.3.5_Linux_x86-64_rpm_langpack_zh-CN.tar.gz

# 查看一下
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ ls
LibreOffice_7.3.5.2_Linux_x86-64_rpm  LibreOffice_7.3.5.2_Linux_x86-64_rpm_langpack_zh-CN

# 安装
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ sudo yum install LibreOffice_7.3.5.2_Linux_x86-64_rpm/RPMS/*.rpm
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ sudo yum install LibreOffice_7.3.5.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/*.rpm

# 安装libcairo.so.2依赖库
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ sudo yum install ibus

# 安装字体
# 在CentOS7服务器上,利用LibreOffice将word等格式转换为PDF,发现不支持汉字。需要安装字体库
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ sudo yum -y install fontconfig
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ sudo yum -y install ttmkfdir
# 检查已有字体库
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ fc-list

# 安装libreoffice-headless
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ sudo yum install -y libreoffice-headless

# 检验是否安装完成
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ libreoffice7.3 --version
LibreOffice 7.3.5.2 184fe81b8c8c30d8b5082578aee2fed2ea847c01

# 测试一下
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ libreoffice --headless --convert-to pdf 1.doc
Error: source file could not be loaded

# 需要安装 libreoffice-writer
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ sudo yum install libreoffice-writer

# 附加安装
yum groupinstall "Fonts"
yum groupinstall "Input Methods"

# 安装路径:/opt/libreoffice7.3
# 快捷方式:/usr/bin/libreoffice7.3

2. 使用LibreOffice

# 转换命令说明
libreoffice --headless --convert-to pdf {
    
    文档路径} --outdir {
    
    导出目录路径}

# 执行实例
soffice --headless --invisible --convert-to pdf --outdir /root/pdf /root/pdf/indicator.xlsx

# 如果是docker安装的,容器外
docker exec libxlsx /bin/bash -c 'soffice --headless --invisible --convert-to pdf --outdir /root/pdf /root/pdf/indicator.xlsx'

# 测试
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ libreoffice --headless --convert-to pdf 222.doc 

[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ ls
222.doc  222.pdf  LibreOffice_7.3.5.2_Linux_x86-64_rpm  LibreOffice_7.3.5.2_Linux_x86-64_rpm_langpack_zh-CN
[harry@iZ8vbbip4bq97z0o60rq3uZ office]$ 

猜你喜欢

转载自blog.csdn.net/qq_31810357/article/details/126221833