undoc 0.2.0

High-performance Microsoft Office document extraction to Markdown
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! # undoc
//!
//! High-performance Microsoft Office document extraction to Markdown.
//!
//! This library provides tools for parsing DOCX, XLSX, and PPTX files
//! and converting them to Markdown, plain text, or structured JSON.
//!
//! ## Quick Start
//!
//! ```no_run
//! use undoc::{parse_file, to_markdown};
//!
//! // Simple text extraction
//! let text = undoc::extract_text("document.docx")?;
//! println!("{}", text);
//!
//! // Convert to Markdown
//! let markdown = to_markdown("document.docx")?;
//! std::fs::write("output.md", markdown)?;
//!
//! // Full parsing with access to structure
//! let doc = parse_file("document.docx")?;
//! println!("Sections: {}", doc.sections.len());
//! println!("Resources: {}", doc.resources.len());
//! # Ok::<(), undoc::Error>(())
//! ```
//!
//! ## Format-Specific APIs
//!
//! ```no_run
//! use undoc::docx::DocxParser;
//! use undoc::xlsx::XlsxParser;
//! use undoc::pptx::PptxParser;
//!
//! // Word documents
//! let doc = DocxParser::open("report.docx")?.parse()?;
//!
//! // Excel spreadsheets
//! let workbook = XlsxParser::open("data.xlsx")?.parse()?;
//!
//! // PowerPoint presentations
//! let presentation = PptxParser::open("slides.pptx")?.parse()?;
//! # Ok::<(), undoc::Error>(())
//! ```
//!
//! ## Features
//!
//! - `docx` (default): Word document support
//! - `xlsx` (default): Excel spreadsheet support
//! - `pptx` (default): PowerPoint presentation support
//! - `async`: Async I/O support with Tokio
//! - `ffi`: C-ABI bindings for foreign language integration

mod charts;
pub mod container;
mod decode;
pub mod detect;
pub mod error;
pub mod model;

#[cfg(feature = "docx")]
pub mod docx;

#[cfg(feature = "xlsx")]
pub mod xlsx;

#[cfg(feature = "pptx")]
pub mod pptx;

pub mod render;

#[cfg(feature = "ffi")]
pub mod ffi;

// Re-exports
pub use container::{OoxmlContainer, Relationship, Relationships};
pub use detect::{detect_format_from_bytes, detect_format_from_path, FormatType};
pub use error::{Error, Result};
pub use model::{
    Block, Cell, CellAlignment, Document, HeadingLevel, ListInfo, ListType, Metadata, Paragraph,
    Resource, ResourceType, Row, Section, Table, TextAlignment, TextRun, TextStyle,
};

use std::path::Path;

/// Parse a document file and return a Document model.
///
/// This function auto-detects the file format and uses the appropriate parser.
///
/// # Example
///
/// ```no_run
/// use undoc::parse_file;
///
/// let doc = parse_file("document.docx")?;
/// println!("Sections: {}", doc.sections.len());
/// # Ok::<(), undoc::Error>(())
/// ```
pub fn parse_file(path: impl AsRef<Path>) -> Result<Document> {
    let path = path.as_ref();
    let format = detect_format_from_path(path)?;

    match format {
        #[cfg(feature = "docx")]
        FormatType::Docx => {
            let mut parser = docx::DocxParser::open(path)?;
            parser.parse()
        }
        #[cfg(feature = "xlsx")]
        FormatType::Xlsx => {
            let mut parser = xlsx::XlsxParser::open(path)?;
            parser.parse()
        }
        #[cfg(feature = "pptx")]
        FormatType::Pptx => {
            let mut parser = pptx::PptxParser::open(path)?;
            parser.parse()
        }
        #[cfg(not(all(feature = "docx", feature = "xlsx", feature = "pptx")))]
        _ => Err(Error::UnsupportedFormat(format!("{:?}", format))),
    }
}

/// Parse a document from bytes.
///
/// # Example
///
/// ```no_run
/// use undoc::parse_bytes;
///
/// let data = std::fs::read("document.docx")?;
/// let doc = parse_bytes(&data)?;
/// # Ok::<(), undoc::Error>(())
/// ```
pub fn parse_bytes(data: &[u8]) -> Result<Document> {
    let format = detect_format_from_bytes(data)?;

    match format {
        #[cfg(feature = "docx")]
        FormatType::Docx => {
            let mut parser = docx::DocxParser::from_bytes(data.to_vec())?;
            parser.parse()
        }
        #[cfg(feature = "xlsx")]
        FormatType::Xlsx => {
            let mut parser = xlsx::XlsxParser::from_bytes(data.to_vec())?;
            parser.parse()
        }
        #[cfg(feature = "pptx")]
        FormatType::Pptx => {
            let mut parser = pptx::PptxParser::from_bytes(data.to_vec())?;
            parser.parse()
        }
        #[cfg(not(all(feature = "docx", feature = "xlsx", feature = "pptx")))]
        _ => Err(Error::UnsupportedFormat(format!("{:?}", format))),
    }
}

/// Extract plain text from a document.
///
/// # Example
///
/// ```no_run
/// use undoc::extract_text;
///
/// let text = extract_text("document.docx")?;
/// println!("{}", text);
/// # Ok::<(), undoc::Error>(())
/// ```
pub fn extract_text(path: impl AsRef<Path>) -> Result<String> {
    let doc = parse_file(path)?;
    Ok(doc.plain_text())
}

/// Convert a document to Markdown.
///
/// # Example
///
/// ```no_run
/// use undoc::to_markdown;
///
/// let markdown = to_markdown("document.docx")?;
/// std::fs::write("output.md", markdown)?;
/// # Ok::<(), undoc::Error>(())
/// ```
pub fn to_markdown(path: impl AsRef<Path>) -> Result<String> {
    let doc = parse_file(path)?;
    render::to_markdown(&doc, &render::RenderOptions::default())
}

/// Convert a document to Markdown with options.
///
/// # Example
///
/// ```no_run
/// use undoc::{to_markdown_with_options, render::RenderOptions};
///
/// let options = RenderOptions::default()
///     .with_frontmatter(true)
///     .with_image_dir("assets");
///
/// let markdown = to_markdown_with_options("document.docx", &options)?;
/// # Ok::<(), undoc::Error>(())
/// ```
pub fn to_markdown_with_options(
    path: impl AsRef<Path>,
    options: &render::RenderOptions,
) -> Result<String> {
    let doc = parse_file(path)?;
    render::to_markdown(&doc, options)
}

/// Convert a document to plain text with render options.
///
/// # Example
///
/// ```no_run
/// use undoc::{to_text, render::RenderOptions};
///
/// let text = to_text("document.docx", &RenderOptions::default())?;
/// # Ok::<(), undoc::Error>(())
/// ```
pub fn to_text(path: impl AsRef<Path>, options: &render::RenderOptions) -> Result<String> {
    let doc = parse_file(path)?;
    render::to_text(&doc, options)
}

/// Convert a document to JSON.
///
/// # Example
///
/// ```no_run
/// use undoc::{to_json, render::JsonFormat};
///
/// let json = to_json("document.docx", JsonFormat::Pretty)?;
/// std::fs::write("output.json", json)?;
/// # Ok::<(), undoc::Error>(())
/// ```
pub fn to_json(path: impl AsRef<Path>, format: render::JsonFormat) -> Result<String> {
    let doc = parse_file(path)?;
    render::to_json(&doc, format)
}

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

    #[test]
    fn test_format_detection_docx() {
        let path = "test-files/file-sample_1MB.docx";
        if Path::new(path).exists() {
            let format = detect_format_from_path(path).unwrap();
            assert_eq!(format, FormatType::Docx);
        }
    }

    #[test]
    fn test_format_detection_xlsx() {
        let path = "test-files/file_example_XLSX_5000.xlsx";
        if Path::new(path).exists() {
            let format = detect_format_from_path(path).unwrap();
            assert_eq!(format, FormatType::Xlsx);
        }
    }

    #[test]
    fn test_format_detection_pptx() {
        let path = "test-files/file_example_PPT_1MB.pptx";
        if Path::new(path).exists() {
            let format = detect_format_from_path(path).unwrap();
            assert_eq!(format, FormatType::Pptx);
        }
    }

    #[test]
    fn test_pptx_to_markdown_with_table() {
        let path = "test-files/file_example_PPT_1MB.pptx";
        if Path::new(path).exists() {
            let md = to_markdown(path).unwrap();

            // Should contain table markdown
            assert!(md.contains("|"), "Output should contain markdown table");

            // Print a portion for inspection
            println!("=== PPTX Markdown Output (Table Section) ===");
            // Find the Table slide and print it
            if let Some(table_start) = md.find("## Slide 3") {
                let table_section = &md[table_start..];
                if let Some(next_slide) = table_section.find("## Slide 4") {
                    println!("{}", &table_section[..next_slide]);
                } else {
                    println!("{}", table_section);
                }
            }
        }
    }

    #[test]
    fn test_pptx_korean_text_spacing() {
        let path = "test-files/고객 응대 시간.pptx";
        if Path::new(path).exists() {
            let md = to_markdown(path).unwrap();

            // Print Slide 1 content
            println!("=== Korean PPTX Output ===");
            if let Some(slide1) = md.find("## Slide 1") {
                let section = &md[slide1..];
                if let Some(next) = section.find("## Slide 2") {
                    println!("{}", &section[..next]);
                } else {
                    println!("{}", section);
                }
            }

            // Proper spacing: "고객 응대 평균 시간 4 시간 ~12 시간 소요"
            // Bad spacing: "고객 응대 평균 시간4시간~12시간 소요"
            assert!(
                !md.contains("시간4시간"),
                "Should have space between '시간' and '4'"
            );
        }
    }

    #[test]
    fn test_all_docx_files() {
        let files = [
            "test-files/file-sample_1MB.docx",
            "test-files/BT-B-24-0017 시험합의서_v0.2.docx",
            "test-files/CJ대한통운_ClusterPlex 로그 분석 보고서_240927-1.docx",
            "test-files/한국농어촌공사(체험마을정보)_OpenAPI활용가이드_v1.0.docx",
        ];

        println!("\n=== DOCX Conversion Report ===\n");
        for path in files {
            if Path::new(path).exists() {
                match parse_file(path) {
                    Ok(doc) => {
                        let md =
                            render::to_markdown(&doc, &render::RenderOptions::default()).unwrap();
                        let text = doc.plain_text();
                        println!("{}", path);
                        println!(
                            "  Sections: {}, Resources: {}",
                            doc.sections.len(),
                            doc.resources.len()
                        );
                        println!(
                            "  Text length: {} chars, MD length: {} chars",
                            text.len(),
                            md.len()
                        );

                        // Check for common issues
                        if md.contains("DOCPROPERTY") || md.contains("HYPERLINK") {
                            println!("  ⚠ Contains field codes (DOCPROPERTY/HYPERLINK)");
                        }
                        if md.contains("\\*") {
                            println!("  ⚠ Contains escaped asterisks (over-escaping)");
                        }
                    }
                    Err(e) => {
                        println!("{}: {}", path, e);
                    }
                }
                println!();
            }
        }
    }

    #[test]
    fn test_all_pptx_files() {
        let files = [
            "test-files/file_example_PPT_1MB.pptx",
            "test-files/고객 응대 시간.pptx",
            "test-files/1. 현장점검  보고서_우수 샘플.pptx",
            "test-files/2차 게이트웨이_20200831 인트세인 현황.pptx",
        ];

        println!("\n=== PPTX Conversion Report ===\n");
        for path in files {
            if Path::new(path).exists() {
                match parse_file(path) {
                    Ok(doc) => {
                        let md =
                            render::to_markdown(&doc, &render::RenderOptions::default()).unwrap();
                        let text = doc.plain_text();
                        println!("{}", path);
                        println!(
                            "  Slides: {}, Resources: {}",
                            doc.sections.len(),
                            doc.resources.len()
                        );
                        println!(
                            "  Text length: {} chars, MD length: {} chars",
                            text.len(),
                            md.len()
                        );

                        // Count tables
                        let table_count = md.matches("| ---").count();
                        if table_count > 0 {
                            println!("  Tables: {}", table_count);
                        }
                    }
                    Err(e) => {
                        println!("{}: {}", path, e);
                    }
                }
                println!();
            }
        }
    }

    #[test]
    fn test_docx_over_escaping() {
        // Test first file
        let path = "test-files/BT-B-24-0017 시험합의서_v0.2.docx";
        if Path::new(path).exists() {
            let md = to_markdown(path).unwrap();
            println!("\n=== Over-escaping Analysis: {} ===\n", path);
            for line in md.lines() {
                if line.contains("\\*") {
                    println!("Found: {}", line);
                }
            }
            assert!(!md.contains("\\*"), "Should not have escaped asterisks");
        }

        // Test second file
        let path2 = "test-files/CJ대한통운_ClusterPlex 로그 분석 보고서_240927-1.docx";
        if Path::new(path2).exists() {
            let md = to_markdown(path2).unwrap();
            println!("\n=== Over-escaping Analysis: {} ===\n", path2);
            for line in md.lines() {
                if line.contains("\\*") {
                    println!("Found: {}", line);
                }
            }
        }
    }

    #[test]
    fn test_all_xlsx_files() {
        let files = [
            "test-files/file_example_XLSX_5000.xlsx",
            "test-files/Auto Expense Report.xlsx",
            "test-files/Basic Invoice.xlsx",
        ];

        println!("\n=== XLSX Conversion Report ===\n");
        for path in files {
            if Path::new(path).exists() {
                match parse_file(path) {
                    Ok(doc) => {
                        let md =
                            render::to_markdown(&doc, &render::RenderOptions::default()).unwrap();
                        let text = doc.plain_text();
                        println!("{}", path);
                        println!("  Sheets: {}", doc.sections.len());
                        println!(
                            "  Text length: {} chars, MD length: {} chars",
                            text.len(),
                            md.len()
                        );

                        // Count table rows
                        let row_count = md.matches("\n|").count();
                        println!("  Table rows: ~{}", row_count);
                    }
                    Err(e) => {
                        println!("{}: {}", path, e);
                    }
                }
                println!();
            }
        }
    }
}