通过java将 word(.doc) 转 md

说明:使用的jdk8的版本

1、添加相关依赖

复制代码
        <!-- Apache POI 处理Word文档 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.2</version>
        </dependency>

2、实现代码

java 复制代码
// 转换 .doc 格式文件
    public void convertDoc(String inputPath, String outputPath) throws IOException {
        try (HWPFDocument document = new HWPFDocument(new FileInputStream(inputPath));
             FileWriter writer = new FileWriter(outputPath)) {

            Range range = document.getRange();
            for (int i = 0; i < range.numParagraphs(); i++) {
                Paragraph para = range.getParagraph(i);
                String text = para.text().trim();
                if (text.isEmpty()) continue;

                // 处理标题
                String mdText = processDocHeading(para, text);
                if (mdText != null) {
                    writer.write(mdText + "\n\n");
                    continue;
                }

                // 处理列表
                mdText = processDocList(para, text);
                if (mdText != null) {
                    writer.write(mdText + "\n\n");
                    continue;
                }

                // 普通段落 自定义过滤条件
                if (!text.contains("页 共")){
                    writer.write(text + "\n\n");
                }
            }
        }
    }

    // 处理 .doc 标题
    private String processDocHeading(Paragraph para, String text) {
        int styleIndex = para.getStyleIndex();
        // 标题样式索引通常为 0-5 对应 1-6 级标题
        if (styleIndex >= 0 && styleIndex <= 5) {
            if (text.contains("?")){//自定义过滤条件
                return repeat("#", styleIndex + 1) + " " + text;//styleIndex + 1表示从标题1开始
            }
        }
        return null;
    }

    // 处理 .doc 列表
    private String processDocList(Paragraph para, String text) {
        // 兼容处理列表判断,避免使用可能不存在的方法
        try {
            // 通过反射检查是否有 getListFormat 方法
            Object listFormat = para.getClass().getMethod("getListFormat").invoke(para);
            if (listFormat != null) {
                // 检查是否有列表编号
                Object listNumber = listFormat.getClass().getMethod("getListNumber").invoke(listFormat);
                if (listNumber instanceof Integer && (Integer) listNumber > 0) {
                    return "- " + text;
                }
            }
        } catch (Exception e) {
            // 没有列表格式,返回 null
        }
        return null;
    }

    @Test
    public  void a() {

        try {
            // 转换.doc文件  第一个参数是word文件所在路径,第二个是希望输出的文件路径及命名
            convertDoc("C:\\***\\***.doc", "D:\\***\\output_from_doc.md");
            log.info("转换完成");
        }catch (Exception e){
            log.error("转换失败:"+e);
        }
    }

3、自此,转换已完成,若想要docx格式的转md 可参考下面

java 复制代码
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumPr;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

public class WordToMarkdown {

    // 转换 .doc 格式文件
    public void convertDoc(String inputPath, String outputPath) throws IOException {
        try (HWPFDocument document = new HWPFDocument(new FileInputStream(inputPath));
             FileWriter writer = new FileWriter(outputPath)) {

            Range range = document.getRange();
            for (int i = 0; i < range.numParagraphs(); i++) {
                Paragraph para = range.getParagraph(i);
                String text = para.text().trim();
                if (text.isEmpty()) continue;

                // 处理标题
                String mdText = processDocHeading(para, text);
                if (mdText != null) {
                    writer.write(mdText + "\n\n");
                    continue;
                }

                // 处理列表
                mdText = processDocList(para, text);
                if (mdText != null) {
                    writer.write(mdText + "\n\n");
                    continue;
                }

                // 普通段落
                writer.write(text + "\n\n");
            }
        }
    }

    // 转换 .docx 格式文件
    public void convertDocx(String inputPath, String outputPath) throws IOException {
        try (XWPFDocument document = new XWPFDocument(new FileInputStream(inputPath));
             FileWriter writer = new FileWriter(outputPath)) {

            for (XWPFParagraph para : document.getParagraphs()) {
                String text = para.getText().trim();
                if (text.isEmpty()) continue;

                // 处理标题
                String mdText = processDocxHeading(para, text);
                if (mdText != null) {
                    writer.write(mdText + "\n\n");
                    continue;
                }

                // 处理列表
                mdText = processDocxList(para, text);
                if (mdText != null) {
                    writer.write(mdText + "\n\n");
                    continue;
                }

                // 普通段落
                writer.write(text + "\n\n");
            }
        }
    }

    // 处理 .doc 标题
    private String processDocHeading(Paragraph para, String text) {
        int styleIndex = para.getStyleIndex();
        // 标题样式索引通常为 0-5 对应 1-6 级标题
        if (styleIndex >= 0 && styleIndex <= 5) {
            return repeat("#", styleIndex + 1) + " " + text;
        }
        return null;
    }

    // 处理 .doc 列表
    private String processDocList(Paragraph para, String text) {
        // 兼容处理列表判断,避免使用可能不存在的方法
        try {
            // 通过反射检查是否有 getListFormat 方法
            Object listFormat = para.getClass().getMethod("getListFormat").invoke(para);
            if (listFormat != null) {
                // 检查是否有列表编号
                Object listNumber = listFormat.getClass().getMethod("getListNumber").invoke(listFormat);
                if (listNumber instanceof Integer && (Integer) listNumber > 0) {
                    return "- " + text;
                }
            }
        } catch (Exception e) {
            // 没有列表格式,返回 null
        }
        return null;
    }

    // 处理 .docx 标题
    private String processDocxHeading(XWPFParagraph para, String text) {
        String styleName = para.getStyle();
        if (styleName != null && styleName.startsWith("Heading")) {
            try {
                int level = Integer.parseInt(styleName.replaceAll("[^0-9]", ""));
                if (level >= 1 && level <= 6) {
                    return repeat("#", level) + " " + text;
                }
            } catch (NumberFormatException e) {
                // 忽略格式错误的标题
            }
        }
        return null;
    }

    // 处理 .docx 列表
    private String processDocxList(XWPFParagraph para, String text) {
        CTP ctp = para.getCTP();
        if (ctp == null) return null;
        
        CTPPr pPr = ctp.getPPr();
        if (pPr == null) return null;
        
        CTNumPr numPr = pPr.getNumPr();
        if (numPr != null) {
            return "- " + text;
        }
        return null;
    }

    // 字符串重复方法(兼容 JDK8)
    private String repeat(String str, int times) {
        if (times <= 0) return "";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < times; i++) {
            sb.append(str);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        WordToMarkdown converter = new WordToMarkdown();
        try {
            // 转换 .doc 文件
            converter.convertDoc("input.doc", "output_doc.md");
            
            // 转换 .docx 文件
            converter.convertDocx("input.docx", "output_docx.md");
            
            System.out.println("转换完成!");
        } catch (IOException e) {
            System.err.println("转换失败:" + e.getMessage());
            e.printStackTrace();
        }
    }
}
相关推荐
ps酷教程7 小时前
Jackson 解决没有无参构造函数的反序列化问题
java
NiceCloud喜云7 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
AI玫瑰助手8 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车8 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋8 小时前
C++14特性
开发语言·c++·c++14特性
_日拱一卒8 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
隔窗听雨眠8 小时前
Nginx网关响应慢排查手记
java·服务器·nginx
智慧物业老杨9 小时前
智慧物业合同周期管理系统:从风险预警到智能交接的全流程数智化落地方案
java·人工智能·python
源码宝9 小时前
MES系统源码:Java8 + SpringBoot2.7 + MySQL8 + Redis,后端源码清爽易扩展
java·后端·源码·springboot·mes系统·源码二开·mes源码
JAVA社区10 小时前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展