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(())
}
相关推荐
forAllforMe17 分钟前
LAN9252 从机寄存器配置--C语言举例
c语言·开发语言
weixin_5375904529 分钟前
《C程序设计语言》练习答案(练习1-4)
c语言·开发语言
chushiyunen1 小时前
python中的内置属性 todo
开发语言·javascript·python
麦麦鸡腿堡1 小时前
JavaWeb_请求参数,设置响应数据,分层解耦
java·开发语言·前端
2301_819414301 小时前
C++与区块链智能合约
开发语言·c++·算法
不想看见4041 小时前
Valid Parentheses栈和队列--力扣101算法题解笔记
开发语言·数据结构·c++
炸膛坦客1 小时前
单片机/C/C++八股:(十五)内存对齐、结构体内存对齐
c语言·开发语言·单片机
娇娇yyyyyy2 小时前
QT编程(13): Qt 事件机制eventfilter
开发语言·qt
bcbobo21cn2 小时前
C# byte类型和byte数组的使用
开发语言·c#·字节数组·byte类型
计算机安禾2 小时前
【C语言程序设计】第37篇:链表数据结构(一):单向链表的实现
c语言·开发语言·数据结构·c++·算法·链表·蓝桥杯