Word 字符数精确统计工具

复制代码
自动 fallback 全量扫描(100%准确)
java 复制代码
import org.apache.poi.ooxml.POIXMLProperties;
import org.apache.poi.xwpf.usermodel.*;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * Word 字符统计工具
 *
 * 功能:
 * 自动 fallback 全量扫描(100%准确)
 *
 * 适用:
 * 上传限制 / 翻译计费 / 文档校验
 */
public class WordCharCounter {

    /**
     * 根据文件路径统计
     */
    public static int count(String path) throws Exception {
        try (InputStream is = new FileInputStream(path)) {
            return count(is);
        }
    }

    /**
     * 根据流统计
     */
    public static int count(InputStream is) throws Exception {

        try (XWPFDocument doc = new XWPFDocument(is)) {

//            // ========= ① 极速模式(读取 metadata) =========
//            int fast = readFastCount(doc);
//
//            if (fast > 0) {
//                return fast;
//            }

            // ========= ② fallback 全量扫描 =========
            return slowCount(doc);
        }
    }

    /**
     * 读取 Office/WPS 预计算字数(极快)
     */
    private static int readFastCount(XWPFDocument doc) {
        try {
            POIXMLProperties.ExtendedProperties props =
                    doc.getProperties().getExtendedProperties();

            return props.getUnderlyingProperties().getCharacters();
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 全量扫描统计(绝对准确)
     */
    private static int slowCount(XWPFDocument doc) {

        int total = 0;

        // 正文
        total += countElements(doc.getBodyElements());

        // header
        for (XWPFHeader header : doc.getHeaderList()) {
            total += countElements(header.getBodyElements());
        }

        // footer
        for (XWPFFooter footer : doc.getFooterList()) {
            total += countElements(footer.getBodyElements());
        }

        return total;
    }

    /**
     * 统计 bodyElements(段落 + 表格)
     */
    private static int countElements(List<IBodyElement> elements) {

        int total = 0;

        for (IBodyElement element : elements) {

            // 段落
            if (element instanceof XWPFParagraph) {
                total += ((XWPFParagraph) element).getText().length();
            }

            // 表格
            else if (element instanceof XWPFTable) {
                XWPFTable table = (XWPFTable) element;

                for (XWPFTableRow row : table.getRows()) {
                    for (XWPFTableCell cell : row.getTableCells()) {
                        total += cell.getText().length();
                    }
                }
            }
        }

        return total;
    }

    // ================= 测试入口 =================

    public static void main(String[] args) throws Exception {

        int count = WordCharCounter.count(
                "d:\\测试解读word内容.docx"
        );

        System.out.println("字符总数: " + count);
    }
}

相关推荐
chatexcel3 小时前
ChatExcel AI文档上线:AI自动生成Word报告的完整工作流
人工智能·word
俊哥工具4 小时前
不用安装不收费!多功能U盘修复工具,解决大部分U盘故障
学习·pdf·word·excel·音视频
草丛中的蝈蝈1 天前
word目录中的一级标题编号和标题之间距离很大,但是内容里是正常的
word
Metaphor6921 天前
使用 Python 设置 Word 文档文本的颜色
python·word
usdoc文档预览1 天前
国产化踩坑:Vue3 / React / 小程序如何免插件实现 OFD 及复杂 Office 文档同屏预览
前端·javascript·react.js·小程序·pdf·word·office文件在线预览
一头爱吃肉的牛1 天前
Word转PPT教程:三步用AI工具一键生成
人工智能·word·powerpoint
熟悉的新风景2 天前
word,wps使用技巧
word·wps
2601_958492552 天前
Webmaster Notes: Deploying HTML5 Word Environments
前端·word·html5
wujian83113 天前
AI表格怎么导出word
人工智能·ai·word·豆包·deepseek·ai导出鸭
zh路西法3 天前
【Word自动目录使用指南】告别手动修改格式,一次设置成功!
word