gpt4 book ai didi

java - 使用 JODConveter 进行 doc 到 docx 转换失败,错误代码 : 2074

转载 作者:太空宇宙 更新时间:2023-11-04 12:08:10 27 4
gpt4 key购买 nike

我需要将 doc 转换为 docx,我正在使用 JODConveter (OpenOffice),但不幸的是我的代码因错误代码 2074 而中断。任何人都可以更深入地了解此错误代码的含义以及我如何修复它。

我的代码分享如下:

OfficeManager officeManager =
new DefaultOfficeManagerConfiguration().setOfficeHome(
new File("C:\\Program Files (x86)\\OpenOffice4")).buildOfficeManager();

officeManager.start();

OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);

DocumentFormat docx = converter.getFormatRegistry().getFormatByExtension("docx");
docx.setStoreProperties(DocumentFamily.TEXT,
Collections.singletonMap("FilterName",
"MS Word 2007 XML"));

converter.convert(new File("C:\\localFiles\\abc.doc"),
new File("C:\\localFiles\\abc_new.docx"));

officeManager.stop();

但是,如果我将预期文件的扩展名从 docx 更改为 pdf,则上述代码可以正常工作。

最佳答案

正如您显然使用的是 Windows,有一个更稳定的解决方案,也将为您提供保真度更高的转换结果。

您必须安装任何版本的 Office(2007 或更高版本)或从 Microsoft 下载并安装兼容包(如果尚未完成)。然后您可以使用以下命令轻松地将 .doc 转换为 .docx:

"C:\Program Files\Microsoft Office\Office12\wordconv.exe" -oice -nme <input file> <output file>

其中<输入文件>和<输出文件>需要是完全限定的路径名​​。

使用 for 可以轻松地将命令应用于多个文档:

for %F in (*.doc) do "C:\Program Files\Microsoft Office\Office12\wordconv.exe" -oice -nme "%F" "%Fx"

或者您可以从 Java 调用该命令:

Process p = Runtime.getRuntime().exec(
new String[] {
"C:\Program Files\Microsoft Office\Office12\wordconv.exe",
"-oice",
"-nme",
"C:\\localFiles\\abc.doc",
"C:\\localFiles\\abc_new.docx"
});
int exitVal = p.waitFor();

关于java - 使用 JODConveter 进行 doc 到 docx 转换失败,错误代码 : 2074,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40135577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com