word转html(批量)

package com.demo.running;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class WordToHtml {
    /**
          方法原型: change(String paths)
    功能描述: 将指定目录下面所有的doc文件转化为HTML并存储在相同目录下
    输入参数: String
    输出参数: 无
    返 回 值: 无
    其它说明: 递归
     **/

    public static boolean change(String FolderPath, String FileName, String outFolder, ActiveXComponent app) {

        String FileFormat = "";
        System.out.println(FolderPath);
        int fileSuffix = 4;
        if (FileName.endsWith("docx")) {
            FileFormat = FileName.substring(FileName.length() - 5, FileName.length());
            fileSuffix = 5;
        } else if (FileName.endsWith("doc")) {
            FileFormat = FileName.substring(FileName.length() - 4, FileName.length());
        }

        System.out.println(FileFormat);

        if (FileFormat.equalsIgnoreCase(".doc") || FileFormat.equalsIgnoreCase(".docx")) {

            String DocFile = FolderPath + "//" + FileName;
            String htmlFile = outFolder + "//" + FileName;

            System.out.println("word文件路径:" + DocFile);
            // word文件的完整路径

            String HtmlFile = htmlFile.substring(0, (htmlFile.length() - fileSuffix)) + ".html";

            System.out.println("htm文件路径:" + HtmlFile);
            // html文件的完整路径
            System.out.println(System.getProperty("java.library.path"));
            // 启动word

            try {
                app.setProperty("Visible", new Variant(false));
                // 设置word程序非可视化运行

                Dispatch docs = app.getProperty("Documents").toDispatch();

                Dispatch doc = Dispatch
                        .invoke(docs, "Open", Dispatch.Method,
                                new Object[] { DocFile, new Variant(false), new Variant(true) }, new int[1])
                        .toDispatch();
                // 打开word文件

                Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { HtmlFile, new Variant(8) }, new int[1]);
                // 作为htm格式保存文件

                Dispatch.call(doc, "Close", new Variant(false));
                // 关闭文件

            } catch (Exception e) {
                e.printStackTrace();
            }
            // 转化完毕
            return true;
        }
        return false;
    }
    
    public static void main(String[] args)
    {
        ActiveXComponent app = new ActiveXComponent("Word.Application");
        String paths = new String("D:/123");
        String pathsOut = new String("D:/123/html");
        File docFiles = new File(paths);
        String[] names = docFiles.list();
        for (int i = 0; i < names.length; i++)
        {
            System.out.println(names[i]);
            if(!names[i].startsWith("~"))
            {
                change(paths, names[i],pathsOut, app);
            }
        }
        app.invoke("Quit", new Variant[] {});
    }

}
 

jacob jar包下载地址(注意里面的dll文件要放在C:\Windows\System32目录里,不然会报错):https://download.csdn.net/my/uploads

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python中的python-docx库来进行批量WordHTML操作。具体步骤如下: 1. 安装python-docx库:可以使用pip安装,命令如下: ``` pip install python-docx ``` 2. 编写Python脚本:使用python-docx库打开Word文档,然后将其转换HTML格式并保存。以下是示例代码: ```python import os from docx import Document # 遍历指定目录下的所有docx文件并转换HTML格式 def batch_docx_to_html(input_folder, output_folder): for file_name in os.listdir(input_folder): if file_name.endswith('.docx'): document = Document(os.path.join(input_folder, file_name)) html_file_name = file_name.replace('.docx', '.html') html_file_path = os.path.join(output_folder, html_file_name) with open(html_file_path, 'w', encoding='utf-8') as f: f.write(document.html) # 示例使用 if __name__ == '__main__': input_folder = './input' output_folder = './output' batch_docx_to_html(input_folder, output_folder) ``` 在上述代码中,batch_docx_to_html函数接收两个参数:输入目录和输出目录。该函数遍历输入目录下的所有docx文件,打开每个文件并将其转换HTML格式,然后将转换后的HTML保存到输出目录下。 3. 运行Python脚本:在命令行中执行以下命令来运行Python脚本: ``` python script.py ``` 其中script.py是你编写的Python脚本文件名。运行脚本后,程序会自动遍历输入目录下的所有docx文件并将其转换HTML格式,然后保存到输出目录下。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值