docparser-office 0.1.0

docparser Office 后端:DOCX / XLSX / PPTX 解析(zip + quick-xml 流式)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! DOCX 解析:ZIP + quick-xml 流式文本提取(与 Python `_docx_raw_stream` 语义对齐)。
//!
//! 架构:
//!   `DocxRawBackend` -> `ParseCapability`:打开 OOXML ZIP,流式读取 `word/document.xml`,
//!   提取段落文本与表格行,跳过结构还原(raw 模式)。

use docparser_core::error::{DocError, Result};
use docparser_core::plugin::{ParseCapability, RawDoc, RawPage};
use quick_xml::events::Event;
use quick_xml::Reader;

use crate::zip_read_str;

/// DOCX raw 后端:跳过结构还原,直接提取文本。
pub struct DocxRawBackend;

impl ParseCapability for DocxRawBackend {
    fn id(&self) -> &str {
        "office-raw"
    }
    fn formats(&self) -> &[&str] {
        &["docx"]
    }
    fn parse(&self, path: &str) -> Result<RawDoc> {
        let file = std::fs::File::open(path)
            .map_err(|e| DocError::corrupt(format!("cannot open {path}: {e}")))?;
        let mut zip = zip::ZipArchive::new(file)
            .map_err(|e| DocError::corrupt(format!("ZIP open failed: {e}")))?;

        let xml = zip_read_str(&mut zip, "word/document.xml")?
            .ok_or_else(|| DocError::corrupt("missing word/document.xml"))?;

        let items = docx_raw_text(&xml)?;

        let page = RawPage {
            name: None,
            items,
            ..Default::default()
        };
        Ok(RawDoc {
            pages: vec![page],
            ..Default::default()
        })
    }
}

/// 从 `word/document.xml` 的 `<w:body>` 中提取 raw 文本项。
fn docx_raw_text(xml: &str) -> Result<Vec<docparser_core::plugin::RawTextItem>> {
    let mut reader = Reader::from_str(xml);
    // 不裁剪文本:空白由消费者 trim(实体两侧空格需保留,见 GeneralRef 分支)
    let mut buf = Vec::new();
    let mut items = Vec::new();

    // 找到 <w:body>
    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) if e.name().local_name().as_ref() == crate::tags::W_BODY => {
                break
            }
            Ok(Event::Eof) => return Err(DocError::corrupt("no w:body found")),
            Err(e) => return Err(DocError::corrupt(format!("XML parse error: {e}"))),
            _ => {}
        }
        buf.clear();
    }
    buf.clear();

    // 流式处理 body 的直接子级
    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                let name = e.name().local_name();
                let name = name.as_ref();
                if name == crate::tags::W_P {
                    let t = extract_para_text(&mut reader)?;
                    let trimmed = t.trim().to_string();
                    if !trimmed.is_empty() {
                        items.push(docparser_core::plugin::RawTextItem {
                            text: trimmed,
                            bbox: None,
                        });
                    }
                } else if name == crate::tags::W_TBL {
                    let rows = extract_table_rows(&mut reader)?;
                    if !rows.is_empty() {
                        let t = rows
                            .iter()
                            .map(|r| r.join(","))
                            .collect::<Vec<_>>()
                            .join("\n");
                        items.push(docparser_core::plugin::RawTextItem {
                            text: t,
                            bbox: None,
                        });
                    }
                } else if name == crate::tags::W_SDT {
                    extract_sdt_items(&mut reader, &mut items)?;
                }
            }
            Ok(Event::End(ref e)) => {
                if e.name().local_name().as_ref() == crate::tags::W_BODY {
                    break;
                }
            }
            Ok(Event::Eof) => break,
            Err(e) => return Err(DocError::corrupt(format!("XML parse error: {e}"))),
            _ => {}
        }
        buf.clear();
    }

    Ok(items)
}

/// 跳过 `<w:sdt>` 内部的 `<w:sdtContent>`,提取其中的 `w:p` 段落。
fn extract_sdt_items<R: std::io::BufRead>(
    reader: &mut Reader<R>,
    items: &mut Vec<docparser_core::plugin::RawTextItem>,
) -> Result<()> {
    let mut buf = Vec::new();
    let mut depth = 1;
    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                depth += 1;
                if e.name().local_name().as_ref() == crate::tags::W_P {
                    let t = extract_para_text(reader)?;
                    let trimmed = t.trim().to_string();
                    if !trimmed.is_empty() {
                        items.push(docparser_core::plugin::RawTextItem {
                            text: trimmed,
                            bbox: None,
                        });
                    }
                }
            }
            Ok(Event::End(ref _e)) => {
                depth -= 1;
                if depth == 0 {
                    break;
                }
            }
            Ok(Event::Eof) => break,
            Err(e) => return Err(DocError::corrupt(format!("XML sdt parse error: {e}"))),
            _ => {}
        }
        buf.clear();
    }
    Ok(())
}

/// 提取 `<w:p>` 段落的文本,处理 w:r/w:t/w:tab/w:br/w:cr。
/// 调用时 reader 刚读过 `<w:p>` Start 事件,退出时 reader 处于 `</w:p>` End 事件之后。
fn extract_para_text<R: std::io::BufRead>(reader: &mut Reader<R>) -> Result<String> {
    let mut out = String::new();
    let mut buf = Vec::new();
    let mut depth = 1;

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                depth += 1;
                match e.name().local_name().as_ref() {
                    crate::tags::W_TAB => out.push('\t'),
                    crate::tags::W_BR | crate::tags::W_CR => out.push('\n'),
                    _ => {}
                }
            }
            Ok(Event::End(ref _e)) => {
                depth -= 1;
                if depth == 0 {
                    break;
                }
            }
            Ok(Event::Empty(ref e)) => match e.name().local_name().as_ref() {
                crate::tags::W_TAB => out.push('\t'),
                crate::tags::W_BR | crate::tags::W_CR => out.push('\n'),
                _ => {}
            },
            Ok(Event::Text(ref e)) => {
                // quick-xml 0.38+:Text 已是解码后的字符串,实体需显式 unescape;
                // 纯空白节点来自 XML 缩进,直接丢弃(原先靠 trim_text 处理)
                let raw = e.xml10_content();
                if !raw.trim().is_empty() {
                    if let Ok(t) = quick_xml::escape::unescape(&raw) {
                        out.push_str(&t);
                    }
                }
            }
            Ok(Event::GeneralRef(ref e)) => crate::push_general_ref(&mut out, e),
            Ok(Event::CData(ref e)) => {
                // CDATA 内容不含实体,直接取字符串
                out.push_str(e.as_ref());
            }
            Err(e) => return Err(DocError::corrupt(format!("XML para parse error: {e}"))),
            _ => {}
        }
        buf.clear();
    }
    Ok(out)
}

/// 提取 `<w:tbl>` 的行列结构,处理嵌套表。
/// 调用时 reader 刚读过 `<w:tbl>` Start 事件,退出时 reader 处于 `</w:tbl>` End 事件之后。
fn extract_table_rows<R: std::io::BufRead>(reader: &mut Reader<R>) -> Result<Vec<Vec<String>>> {
    let mut rows: Vec<Vec<String>> = Vec::new();
    let mut current_row: Vec<String> = Vec::new();
    let mut current_cell = String::new();
    let mut tbl_depth = 1;
    let mut buf = Vec::new();
    let mut cell_has_content = false;

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                tbl_depth += 1;
                match e.name().local_name().as_ref() {
                    crate::tags::W_P => {
                        // extract_para_text 在内部消费了 </w:p>,需补偿深度
                        let t = extract_para_text(reader)?;
                        tbl_depth -= 1;
                        if !t.trim().is_empty() {
                            if cell_has_content {
                                current_cell.push('\n');
                            }
                            current_cell.push_str(t.trim());
                            cell_has_content = true;
                        }
                        continue;
                    }
                    crate::tags::W_TBL => {
                        // 嵌套表:递归提取,其内部也消费了 </w:tbl>
                        let nested = extract_table_rows(reader)?;
                        tbl_depth -= 1;
                        if !nested.is_empty() {
                            if cell_has_content {
                                current_cell.push('\n');
                            }
                            current_cell.push_str(
                                &nested
                                    .iter()
                                    .map(|r| r.join(","))
                                    .collect::<Vec<_>>()
                                    .join("\n"),
                            );
                            cell_has_content = true;
                        }
                        continue;
                    }
                    _ => {}
                }
            }
            Ok(Event::End(ref e)) => {
                tbl_depth -= 1;
                match e.name().local_name().as_ref() {
                    crate::tags::W_TBL => {
                        if tbl_depth == 0 {
                            if !current_row.is_empty() {
                                rows.push(std::mem::take(&mut current_row));
                            }
                            break;
                        }
                    }
                    crate::tags::W_TR => {
                        // 行结束:单元格已由 W_TC End 处理,只需保存行
                        if !current_row.is_empty() {
                            rows.push(std::mem::take(&mut current_row));
                        }
                    }
                    crate::tags::W_TC => {
                        // 单元格结束:保存内容
                        if cell_has_content {
                            current_row.push(current_cell.trim().to_string());
                        } else {
                            current_row.push(String::new());
                        }
                        current_cell.clear();
                        cell_has_content = false;
                    }
                    _ => {}
                }
            }
            Ok(Event::Eof) => break,
            Err(e) => return Err(DocError::corrupt(format!("XML table parse error: {e}"))),
            _ => {}
        }
        buf.clear();
    }
    Ok(rows)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_para_simple() {
        let xml = r#"<w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:p><w:r><w:t>Hello</w:t></w:r></w:p>
</w:body>"#;
        let items = docx_raw_text(xml).unwrap();
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].text, "Hello");
    }

    #[test]
    fn test_para_tab_br() {
        let xml = concat!(
            r#"<w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">"#,
            r#"  <w:p><w:r><w:t>a</w:t></w:r><w:tab/><w:r><w:t>b</w:t></w:r><w:br/><w:r><w:t>c</w:t></w:r></w:p>"#,
            r#"</w:body>"#
        );
        let items = docx_raw_text(xml).unwrap();
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].text, "a\tb\nc");
    }

    #[test]
    fn test_simple_table() {
        let xml = concat!(
            r#"<w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">"#,
            r#"  <w:tbl>"#,
            r#"    <w:tr><w:tc><w:p><w:r><w:t>A</w:t></w:r></w:p></w:tc><w:tc><w:p><w:r><w:t>B</w:t></w:r></w:p></w:tc></w:tr>"#,
            r#"  </w:tbl>"#,
            r#"</w:body>"#
        );
        let items = docx_raw_text(xml).unwrap();
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].text, "A,B");
    }

    #[test]
    fn test_nested_table() {
        let xml = concat!(
            r#"<w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">"#,
            r#"  <w:tbl>"#,
            r#"    <w:tr>"#,
            r#"      <w:tc><w:p><w:r><w:t>X</w:t></w:r></w:p></w:tc>"#,
            r#"      <w:tc>"#,
            r#"        <w:p><w:r><w:t>Y</w:t></w:r></w:p>"#,
            r#"        <w:tbl>"#,
            r#"          <w:tr><w:tc><w:p><w:r><w:t>innerA</w:t></w:r></w:p></w:tc>"#,
            r#"               <w:tc><w:p><w:r><w:t>innerB</w:t></w:r></w:p></w:tc></w:tr>"#,
            r#"        </w:tbl>"#,
            r#"      </w:tc>"#,
            r#"    </w:tr>"#,
            r#"  </w:tbl>"#,
            r#"</w:body>"#
        );
        let items = docx_raw_text(xml).unwrap();
        assert_eq!(items.len(), 1);
        assert!(items[0].text.contains("innerA"));
        assert!(items[0].text.contains("innerB"));
    }

    #[test]
    fn test_multi_para() {
        let xml = concat!(
            r#"<w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">"#,
            r#"  <w:p><w:r><w:t>Para1</w:t></w:r></w:p>"#,
            r#"  <w:p><w:r><w:t>Para2</w:t></w:r></w:p>"#,
            r#"</w:body>"#
        );
        let items = docx_raw_text(xml).unwrap();
        assert_eq!(items.len(), 2);
        assert_eq!(items[0].text, "Para1");
        assert_eq!(items[1].text, "Para2");
    }

    /// reader 迁移 smoke:现场造一个最小 docx,走完整 ZIP → 事件流路径。
    ///
    /// 纯函数测试覆盖不到 `name().local_name()` 与显式 `escape::unescape`,
    /// 这里用一个真实 zip 包证明事件解析没被改坏(含实体 `&amp;` 解码)。
    #[test]
    fn smoke_parse_minimal_docx_zip() {
        use std::io::Write;

        const DOCUMENT_XML: &str = concat!(
            r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"#,
            r#"<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">"#,
            r#"<w:body>"#,
            r#"<w:p><w:r><w:t>Smoke 文本</w:t></w:r></w:p>"#,
            r#"<w:p><w:r><w:t>a &amp; b</w:t></w:r></w:p>"#,
            r#"</w:body></w:document>"#
        );

        let dir = std::env::temp_dir().join("docparser_office_docx_smoke");
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("min.docx");
        {
            let f = std::fs::File::create(&path).unwrap();
            let mut zw = zip::ZipWriter::new(f);
            let opts = zip::write::SimpleFileOptions::default();
            zw.start_file("word/document.xml", opts).unwrap();
            zw.write_all(DOCUMENT_XML.as_bytes()).unwrap();
            zw.finish().unwrap();
        }

        let raw = DocxRawBackend.parse(path.to_str().unwrap()).unwrap();
        std::fs::remove_file(&path).ok();

        assert_eq!(raw.pages.len(), 1);
        assert_eq!(raw.pages[0].items.len(), 2);
        assert_eq!(raw.pages[0].items[0].text, "Smoke 文本");
        assert_eq!(raw.pages[0].items[1].text, "a & b");
    }
}