rust的docx-rs库读取docx文件中的文本内容(逐行注释)

一、安装 docx-rs

1、 命令行

bash 复制代码
cargo add docx-rs

2、 Cargo.toml 文件

toml 复制代码
[dependencies]
docx-rs = "0.4"

二、打开读取docx文件

rust 复制代码
let docx = read_docx(include_bytes!("../template.docx"))?;

注:include_bytes!()读取文件,必输使用字符串字面量,就是直接双引号包裹,需要动态路径时候使用一下方式

rust 复制代码
let docx = read_docx(&fs::read(template_path)?)?;

三、读取段落、表格内容完整代码

rust 复制代码
use docx_rs::{read_docx};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 读取docx文件
    let docx = read_docx(include_bytes!("../example.docx"))?;

    // 遍历文档中的元素
    for child in docx.document.children {
        match child {
            // 内容为文字段落
            docx_rs::DocumentChild::Paragraph(para) => {
                for para_child in para.children {
                    match para_child {
                        // 与表格读取相同的一部分内容
                        docx_rs::ParagraphChild::Run(run) => {
                            for run_child in run.children {
                                match run_child {
                                    // 文本内容
                                    docx_rs::RunChild::Text(text) => {
                                        print!("{}", text.text);
                                    }
                                    _ => {}
                                }
                            }
                        }
                        _ => {}
                    }
                }
                println!();
            }
            // 内容为表格
            docx_rs::DocumentChild::Table(table) => {
                // 遍历表格行
                for row in table.rows {
                    if let docx_rs::TableChild::TableRow(table_row) = row {
                        // 行中的元素
                        for cell in table_row.cells {
                            if let docx_rs::TableRowChild::TableCell(cell) = cell {
                                // 单元格中的内容
                               for child in cell.children {
                                   if let docx_rs::TableCellContent::Paragraph(para) = child {
                                       // 表格内的段落元素(以下同段落的读取)
                                       for para_child in para.children {
                                           if let docx_rs::ParagraphChild::Run(run) =para_child{
                                               // 文本集
                                               for run_child in run.children {
                                                   if let docx_rs::RunChild::Text(text) =run_child{
                                                       print!("{}  ",text.text);
                                                   }
                                               }
                                           }
                                       }
                                   }
                               }
                            }
                        }
                    }
                    println!();
                }
            }
            _ => {
                println!("非段落、表格内容: {:?}", child);
            }
        }
    }
    Ok(())
}
相关推荐
Cloud_Shy61811 分钟前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 30 - 32)
开发语言·人工智能·笔记·python·学习方法
天佑木枫37 分钟前
15天Python入门系列 · 序
开发语言·python
宋拾壹2 小时前
同时添加多个类目
android·开发语言·javascript
凡人叶枫2 小时前
Effective C++ 条款04:确定对象被使用前已先被初始化
java·linux·开发语言·c++·嵌入式开发
小小龙学IT2 小时前
Go 语言后端开发:从并发模型到生产落地的工程实践
开发语言·后端·golang
ytttr8733 小时前
Qt 数字键盘实现
开发语言·qt
wearegogog1233 小时前
C# .NET 文件比较工具 WinForms
开发语言·c#·.net
再写一行代码就下班3 小时前
Cursor配置Java环境、创建Spring Boot项目的步骤
java·开发语言·spring boot
零陵上将军_xdr3 小时前
后端转全栈学习-Day5-JavaScript 基础-3
开发语言·javascript·学习
oqX0Cazj23 小时前
2026超火Go-Zero实战:从架构原理到高并发接口落地,彻底解决接口超时、雪崩问题
开发语言·架构·golang