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(())
}
相关推荐
吴声子夜歌4 分钟前
Java——数组和集合(一)
java·开发语言
呆呆敲代码的小Y11 分钟前
Lua 上值(UpValue)
开发语言·junit·lua·lua上值·upvalue
呆呆敲代码的小Y15 分钟前
【游戏开发】C# 中的迭代器
java·开发语言·c#·迭代器
benchmark_cc18 分钟前
K 线数据断层会对回测造成什么影响?从一个缺失交易日看懂量化策略为什么会失真
大数据·开发语言·数据分析·量化·股票数据·quantdash·量化数据源
benchmark_cc23 分钟前
数据 API 稳定性为什么会影响量化策略?从数据获取到信号执行的完整分析
开发语言·python·数据分析·量化·股票数据·quantdash·量化数据源
2601_9621803337 分钟前
python——Django 框架
开发语言·python·django
长江后浪博客37 分钟前
Conda环境下测试Intel NPU:Python版本如何选择?
开发语言·python·conda·openvino·intel npu
Chester_19991 小时前
CSP202206C.角色授权
开发语言·数据结构·c++·蓝桥杯
晴天的雨.9921 小时前
类和对象下(内部类,匿名对象,对象拷贝时的编译器优化)
开发语言·c++·算法
其实防守也摸鱼1 小时前
智能体推荐:精选 AI Agent 工具与实战指南
运维·开发语言·人工智能·学习·web安全·自动化