Linux下OpenOffice安装和转换格式

偶然的机会测试了下OpenOffice的转换功能,记录下过程。

安装OpenOffice组件(以4.1.2版本为例)

1. 下载

OpenOffice官网下载相应的版本。需要注意的是rpm包主要用于redhat及分支如redhat,centos,Fedora等,而deb包主要用于debian及分支如debian,ubuntu等。

2. 解压包

1
tar zxvf Apache_OpenOffice_4.1.2_Linux_x86-64_install-deb_zh-CN.tar.gz

3. 通过包管理器安装

1
2
3
4
#redhat分支
rpm -ivh zh-CN/RPMS/*.rpm
#debain分支
sudo dpkg -i zh-CN/DEBS/*.deb
1
2
3
4
5
6
7
#【以下内容为可选安装】
#作为桌面版使用的安装desktop-integration目录下对应的包可生成相应的快捷方式
#redhat分支
rpm -ivh zh-CN/RPMS/desktop-integration/openoffice4.1.2-redhat-menus-4.1.2-9782.noarch.rpm
#debain分支(第一行解决与libreoffice冲突)
sudo apt-get purge libreoffice*
sudo dpkg -i zh-CN/DEBS/desktop-integration/*.deb

测试

1. 服务启动测试

安装后主目录在/opt/openoffice4下,注意OpenOffice需要Java环境支持
尝试以后台服务方式启动监听,执行完毕无错误仅返回进程号

1
/opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

查看是否存在8100的监听端口

1
netstat -nlt

2. 转换测试

若服务启动正常则开始转换测试,可以在后台保持监听服务或者仅在转换时启动服务。
这里参考着改写了一个仅在转换时启动服务的Java版本测试程序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
*依赖jurt、juh、ridl、unoil、jodconverter-core、commons-io库,前四个库可在OpenOffice主目录中找到
*依赖库打包http://download.csdn.net/detail/tuzhis/9377605
*转换后在相同目录下生成对应文件
*需要转换成其他类型的改程序里对应扩展名即可。
*/
import java.io.File;
import java.util.regex.Pattern;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;

public class WordToPdf {
public static void main(String[] args) {
word2pdf("/home/tu/works/test.doc");
System.out.println("finish");
}

public static void word2pdf(String inputFilePath) {
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();

String officeHome = getOfficeHome();
config.setOfficeHome(officeHome);

OfficeManager officeManager = config.buildOfficeManager();
officeManager.start();

OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
String outputFilePath = getOutputFilePath(inputFilePath);
File inputFile = new File(inputFilePath);
if (inputFile.exists()) {
File outputFile = new File(outputFilePath);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
converter.convert(inputFile, outputFile);
}

officeManager.stop();
}

public static String getOutputFilePath(String inputFilePath) {
String outputFilePath = inputFilePath.replaceAll(".doc", ".html");
return outputFilePath;
}

public static String getOfficeHome() {
String osName = System.getProperty("os.name");
if (Pattern.matches("Linux.*", osName)) {
return "/opt/openoffice4";
} else if (Pattern.matches("Windows.*", osName)) {
return "D:/Program Files/OpenOffice4";
} else if (Pattern.matches("Mac.*", osName)) {
return "/Application/OpenOffice.org.app/Contents";
}
return null;
}

}

FAQ

  1. 直接利用后台服务转换的Java程序片段见第二个参考网址
  2. 直接利用后台服务转换的Python程序片段见第三个参考网址
  3. 一般转换还会用到swftools,见补充

补充

1
2
3
4
5
6
7
8
9
10
#swftools安装
wget http://www.swftools.org/swftools-2013-04-09-1007.tar.gz
tar zxvf swftools-2013-04-09-1007.tar.gz
cd swftools-2013-04-09-1007
./configure --prefix=/usr/swftools
make
sudo make install
#设置环境变量并立即生效
sudo echo 'export PATH=$PATH:/usr/swftools/bin/' >>/etc/profile
source /etc/profile

参考

http://titanseason.iteye.com/blog/1471606
http://blog.csdn.net/niuhea/article/details/7749684
http://blog.csdn.net/catoop/article/details/42527349