docling 0.52.1

DocumentConverter and format backends for docling.rs (a Rust port of docling).
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! XLSX drawings, charts, and cell comments — the non-grid halves of docling's
//! `MsExcelDocumentBackend`.
//!
//! Drawings (`xl/drawings/drawingN.xml`) anchor images and chart frames to
//! cell ranges: a `twoCellAnchor` spans `from..to` (docling's bbox is
//! `(from.col, from.row, to.col+1, to.row+1)`), a `oneCellAnchor` covers a
//! single cell. Charts (`xl/charts/chartN.xml`) carry their series as
//! *references* back into the workbook (`'Sheet1'!$B$2:$B$7`), which docling
//! resolves against the live cell values; the reconstructed grid (categories
//! down the first column as row headers, one column per series) becomes the
//! picture's tabular-chart annotation. Comments pair the legacy
//! `xl/commentsN.xml` part with the Excel-365 threaded-comment XML, preferring
//! the latter's author/timestamp.

use std::collections::HashMap;

use roxmltree::{Document, Node as XmlNode};

/// A drawing anchor: what it holds and its cell-range bbox
/// `(left_col, top_row, right_col_excl, bottom_row_excl)`.
pub struct DrawingItem {
    pub bbox: (usize, usize, usize, usize),
    pub kind: DrawingKind,
}

pub enum DrawingKind {
    /// `<xdr:pic>` with an `<a:blip r:embed>` relationship id.
    Image(String),
    /// `<xdr:graphicFrame>` referencing a chart part by relationship id.
    Chart(String),
}

/// Parse a spreadsheet drawing part into its anchored items, in document order.
pub fn parse_drawing(xml: &str) -> Vec<DrawingItem> {
    let Ok(dom) = Document::parse(xml) else {
        return Vec::new();
    };
    let mut out = Vec::new();
    for anchor in dom
        .root_element()
        .children()
        .filter(|n| matches!(n.tag_name().name(), "twoCellAnchor" | "oneCellAnchor"))
    {
        let cell = |tag: &str| -> Option<(usize, usize)> {
            let n = anchor.children().find(|c| c.has_tag_name(tag))?;
            let num = |t: &str| {
                n.children()
                    .find(|c| c.has_tag_name(t))
                    .and_then(|c| c.text())
                    .and_then(|s| s.trim().parse::<usize>().ok())
            };
            Some((num("col")?, num("row")?))
        };
        let Some((fc, fr)) = cell("from") else {
            continue;
        };
        let bbox = match cell("to") {
            Some((tc, tr)) => (fc, fr, tc + 1, tr + 1),
            None => (fc, fr, fc + 1, fr + 1),
        };
        let kind = if let Some(blip) = anchor.descendants().find(|n| {
            n.has_tag_name("blip") && !n.ancestors().any(|a| a.has_tag_name("graphicFrame"))
        }) {
            match blip.attributes().find(|a| a.name() == "embed") {
                Some(a) => DrawingKind::Image(a.value().to_string()),
                None => continue,
            }
        } else if let Some(chart) = anchor.descendants().find(|n| n.has_tag_name("chart")) {
            match chart.attributes().find(|a| a.name() == "id") {
                Some(a) => DrawingKind::Chart(a.value().to_string()),
                None => continue,
            }
        } else {
            continue;
        };
        out.push(DrawingItem { bbox, kind });
    }
    out
}

/// A chart's declarative content: docling's classification label, the title
/// (caption), and the series with their workbook references.
pub struct ChartSpec {
    pub kind: &'static str,
    pub title: Option<String>,
    pub series: Vec<SeriesSpec>,
}

pub struct SeriesSpec {
    /// The series name: a resolvable reference, or a literal value.
    pub name_ref: Option<String>,
    pub name_lit: Option<String>,
    /// Categories (`c:cat` / `c:xVal`) reference.
    pub cat_ref: Option<String>,
    /// Values (`c:val` / `c:yVal`) reference.
    pub val_ref: Option<String>,
    /// Cached category/value points (`strCache`/`numCache` `c:pt` entries) —
    /// the only data DOCX/PPTX charts carry (no workbook to resolve against).
    pub cat_cache: Vec<String>,
    pub val_cache: Vec<String>,
    /// Cached series name (the `c:tx` reference's `strCache`).
    pub name_cache: Option<String>,
}

/// docling's `_CHART_TAGNAME_TO_CLASSIFICATION`.
fn classification(tag: &str) -> Option<&'static str> {
    Some(match tag {
        "barChart" | "bar3DChart" => "bar_chart",
        "lineChart" | "line3DChart" => "line_chart",
        "pieChart" | "pie3DChart" | "doughnutChart" => "pie_chart",
        "scatterChart" => "scatter_chart",
        "areaChart" | "area3DChart" => "other_chart",
        _ => return None,
    })
}

/// Parse `xl/charts/chartN.xml` into a [`ChartSpec`]. The chart *kind* comes
/// from the first plot-area child docling's map knows (unknown kinds fall back
/// to `other_chart` when any `*Chart` element exists).
pub fn parse_chart(xml: &str) -> Option<ChartSpec> {
    let dom = Document::parse(xml).ok()?;
    let plot = dom.descendants().find(|n| n.has_tag_name("plotArea"))?;
    let chart_el = plot
        .children()
        .find(|n| n.tag_name().name().ends_with("Chart"))?;
    let kind = classification(chart_el.tag_name().name()).unwrap_or("other_chart");

    // Title: all `<a:t>` runs under `c:title`, concatenated.
    let title = dom
        .descendants()
        .find(|n| n.has_tag_name("title"))
        .map(|t| {
            t.descendants()
                .filter(|n| n.has_tag_name("t"))
                .filter_map(|n| n.text())
                .collect::<String>()
        })
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty());

    // A reference formula from a `c:tx`/`c:cat`/`c:val`-style node: its
    // `numRef`/`strRef` child's `c:f` text (docling's `_ref_formula`).
    let ref_formula = |node: XmlNode| -> Option<String> {
        node.children()
            .find(|c| matches!(c.tag_name().name(), "numRef" | "strRef"))
            .and_then(|r| r.children().find(|c| c.has_tag_name("f")))
            .and_then(|f| f.text())
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
    };

    let mut series = Vec::new();
    for ser in chart_el.children().filter(|n| n.has_tag_name("ser")) {
        let child = |tag: &str| ser.children().find(|c| c.has_tag_name(tag));
        let name_ref = child("tx").and_then(ref_formula);
        let name_lit = child("tx")
            .and_then(|tx| tx.children().find(|c| c.has_tag_name("v")))
            .and_then(|v| v.text())
            .map(str::to_string);
        let cat_ref = child("cat")
            .and_then(ref_formula)
            .or_else(|| child("xVal").and_then(ref_formula));
        let val_ref = child("val")
            .and_then(ref_formula)
            .or_else(|| child("yVal").and_then(ref_formula));
        let cat_cache = child("cat")
            .map(cache_points)
            .filter(|v| !v.is_empty())
            .or_else(|| child("xVal").map(cache_points))
            .unwrap_or_default();
        let val_cache = child("val")
            .map(cache_points)
            .filter(|v| !v.is_empty())
            .or_else(|| child("yVal").map(cache_points))
            .unwrap_or_default();
        let name_cache = child("tx")
            .map(cache_points)
            .and_then(|v| v.into_iter().next());
        series.push(SeriesSpec {
            name_ref,
            name_lit,
            cat_ref,
            val_ref,
            cat_cache,
            val_cache,
            name_cache,
        });
    }
    Some(ChartSpec {
        kind,
        title,
        series,
    })
}

/// The cached points under a `c:cat`/`c:val`/`c:tx` node: every `c:pt`'s
/// `c:v` inside its `strCache`/`numCache`, ordered by the point index.
/// Numbers render openpyxl-style (no trailing `.0`), matching the cell
/// formatter — DOCX/PPTX charts carry their data only in these caches.
fn cache_points(node: XmlNode) -> Vec<String> {
    let Some(cache) = node
        .descendants()
        .find(|c| matches!(c.tag_name().name(), "strCache" | "numCache"))
    else {
        return Vec::new();
    };
    let mut pts: Vec<(usize, String)> = cache
        .children()
        .filter(|c| c.has_tag_name("pt"))
        .filter_map(|pt| {
            let idx: usize = pt.attribute("idx")?.parse().ok()?;
            let v = pt.children().find(|c| c.has_tag_name("v"))?.text()?;
            Some((idx, format_cached_value(v)))
        })
        .collect();
    pts.sort_by_key(|(i, _)| *i);
    pts.into_iter().map(|(_, v)| v).collect()
}

/// A cached value, numbers normalized like openpyxl's `str(value)` (an
/// integer-valued float loses its `.0`).
fn format_cached_value(v: &str) -> String {
    match v.trim().parse::<f64>() {
        Ok(f) if f.is_finite() && f.fract() == 0.0 && f.abs() < 1e15 => {
            format!("{}", f as i64)
        }
        Ok(f) => format!("{f}"),
        Err(_) => v.to_string(),
    }
}

/// docling's `_chart_to_table_data` grid from resolved series columns:
/// categories down the first column (row headers), one column per series
/// (column headers), the top-left cell empty. Shared by the XLSX backend
/// (which resolves references into sheets) and the DOCX/PPTX backends
/// (which use the embedded caches).
pub fn chart_table_from_columns(
    categories: Vec<String>,
    columns: Vec<(String, Vec<String>)>,
) -> Option<docling_core::Table> {
    let num_data_rows = columns
        .iter()
        .map(|(_, v)| v.len())
        .chain([categories.len()])
        .max()
        .unwrap_or(0);
    if num_data_rows == 0 || columns.is_empty() {
        return None;
    }
    let mut rows: Vec<Vec<String>> = Vec::new();
    let mut header = vec![String::new()];
    header.extend(columns.iter().map(|(n, _)| n.clone()));
    rows.push(header);
    for i in 0..num_data_rows {
        let mut row = vec![categories.get(i).cloned().unwrap_or_default()];
        for (_, values) in &columns {
            row.push(values.get(i).cloned().unwrap_or_default());
        }
        rows.push(row);
    }
    let nrows = rows.len();
    let ncols = rows[0].len();
    let mut header_row = vec![false; nrows];
    header_row[0] = true;
    let mut row_header = vec![vec![false; ncols]; nrows];
    for r in row_header.iter_mut().skip(1) {
        r[0] = true;
    }
    Some(docling_core::Table {
        rows,
        location: None,
        structure: Some(docling_core::TableStructure {
            header_row,
            col_continuation: Vec::new(),
            row_continuation: Vec::new(),
            row_header,
            col_header: Vec::new(),
        }),
        cell_blocks: None,
    })
}

/// A chart's data table built purely from the embedded caches (the DOCX/PPTX
/// path — no workbook). `None` when the chart carries no cached data.
pub fn chart_table_from_caches(spec: &ChartSpec) -> Option<docling_core::Table> {
    if spec.series.is_empty() {
        return None;
    }
    let categories = spec
        .series
        .iter()
        .map(|s| s.cat_cache.clone())
        .find(|c| !c.is_empty())
        .unwrap_or_default();
    let columns: Vec<(String, Vec<String>)> = spec
        .series
        .iter()
        .map(|s| {
            let name = s
                .name_cache
                .clone()
                .or_else(|| s.name_lit.clone())
                .unwrap_or_default();
            (name, s.val_cache.clone())
        })
        .collect();
    chart_table_from_columns(categories, columns)
}

/// 0-based inclusive range bounds `(min_col, min_row, max_col, max_row)`.
pub type RangeBounds = (usize, usize, usize, usize);

/// Split a range reference (`'Duck Observations'!$B$2:$B$7`) into the sheet
/// name (unquoted, `''` unescaped; `None` when unqualified) and its bounds.
pub fn parse_range_ref(reference: &str) -> Option<(Option<String>, RangeBounds)> {
    let (sheet, cells) = match reference.rsplit_once('!') {
        Some((s, c)) => {
            let s = s.trim();
            let name = if s.starts_with('\'') && s.ends_with('\'') && s.len() >= 2 {
                s[1..s.len() - 1].replace("''", "'")
            } else {
                s.to_string()
            };
            (Some(name), c)
        }
        None => (None, reference),
    };
    let mut corners = cells.split(':');
    let a = cell_ref(corners.next()?)?;
    let b = match corners.next() {
        Some(c) => cell_ref(c)?,
        None => a,
    };
    Some((
        sheet,
        (a.0.min(b.0), a.1.min(b.1), a.0.max(b.0), a.1.max(b.1)),
    ))
}

/// `$B$7` → 0-based `(col, row)`.
fn cell_ref(s: &str) -> Option<(usize, usize)> {
    let s = s.replace('$', "");
    let letters: String = s.chars().take_while(|c| c.is_ascii_alphabetic()).collect();
    let digits: String = s.chars().skip_while(|c| c.is_ascii_alphabetic()).collect();
    if letters.is_empty() || digits.is_empty() {
        return None;
    }
    let col = letters.chars().fold(0usize, |acc, c| {
        acc * 26 + (c.to_ascii_uppercase() as usize - 'A' as usize + 1)
    });
    Some((col - 1, digits.parse::<usize>().ok()? - 1))
}

/// Parse the legacy comments part (`xl/commentsN.xml`) into per-cell
/// `(ref, author, text)` entries, in part order.
pub fn parse_legacy_comments(xml: &str) -> Vec<(String, String, String)> {
    let Ok(dom) = Document::parse(xml) else {
        return Vec::new();
    };
    let authors: Vec<String> = dom
        .descendants()
        .find(|n| n.has_tag_name("authors"))
        .map(|a| {
            a.children()
                .filter(|c| c.has_tag_name("author"))
                .map(|c| c.text().unwrap_or("").to_string())
                .collect()
        })
        .unwrap_or_default();
    let mut out = Vec::new();
    for c in dom.descendants().filter(|n| n.has_tag_name("comment")) {
        let cell = c
            .attributes()
            .find(|a| a.name() == "ref")
            .map(|a| a.value().to_string())
            .unwrap_or_default();
        let author = c
            .attributes()
            .find(|a| a.name() == "authorId")
            .and_then(|a| a.value().parse::<usize>().ok())
            .and_then(|i| authors.get(i).cloned())
            .unwrap_or_default();
        let text: String = c
            .descendants()
            .filter(|n| n.has_tag_name("t"))
            .filter_map(|n| n.text())
            .collect();
        out.push((cell, author, text.trim().to_string()));
    }
    out
}

/// Parse a threaded-comments part into `ref -> (author, text, time)` using the
/// persons map (`personId -> displayName`).
pub fn parse_threaded_comments(
    xml: &str,
    persons: &HashMap<String, String>,
) -> HashMap<String, (String, String, Option<String>)> {
    let Ok(dom) = Document::parse(xml) else {
        return HashMap::new();
    };
    let mut out = HashMap::new();
    for c in dom
        .descendants()
        .filter(|n| n.has_tag_name("threadedComment"))
    {
        let attr = |name: &str| {
            c.attributes()
                .find(|a| a.name() == name)
                .map(|a| a.value().to_string())
        };
        let Some(cell) = attr("ref") else { continue };
        let author = attr("personId")
            .and_then(|id| persons.get(&id).cloned())
            .unwrap_or_else(|| "Unknown".to_string());
        let text = c
            .children()
            .find(|n| n.has_tag_name("text"))
            .and_then(|t| t.text())
            .unwrap_or("")
            .to_string();
        let time = attr("dT").map(|t| format_comment_time(&t));
        out.insert(cell, (author, text, time));
    }
    out
}

/// `xl/persons/person.xml` → `id -> displayName`.
pub fn parse_persons(xml: &str) -> HashMap<String, String> {
    let Ok(dom) = Document::parse(xml) else {
        return HashMap::new();
    };
    dom.descendants()
        .filter(|n| n.has_tag_name("person"))
        .filter_map(|p| {
            let get = |name: &str| {
                p.attributes()
                    .find(|a| a.name() == name)
                    .map(|a| a.value().to_string())
            };
            Some((get("id")?, get("displayName")?))
        })
        .collect()
}

/// A threaded comment's `dT` timestamp rendered like docling — Python's
/// `datetime.isoformat(timespec="milliseconds")`: the fraction padded/truncated
/// to exactly three digits, a `Z` suffix becoming `+00:00`.
fn format_comment_time(raw: &str) -> String {
    let (base, tz) = match raw.strip_suffix('Z') {
        Some(b) => (b, "+00:00"),
        None => (raw, ""),
    };
    let (secs, frac) = match base.split_once('.') {
        Some((s, f)) => (s, f),
        None => (base, ""),
    };
    let mut ms = frac.to_string();
    ms.truncate(3);
    while ms.len() < 3 {
        ms.push('0');
    }
    format!("{secs}.{ms}{tz}")
}

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

    #[test]
    fn range_refs() {
        assert_eq!(
            parse_range_ref("'Duck Observations'!$B$2:$B$7"),
            Some((Some("Duck Observations".to_string()), (1, 1, 1, 6)))
        );
        assert_eq!(
            parse_range_ref("Sheet1!$A$1"),
            Some((Some("Sheet1".to_string()), (0, 0, 0, 0)))
        );
        assert_eq!(cell_ref("$AB$10"), Some((27, 9)));
    }

    #[test]
    fn comment_time() {
        assert_eq!(
            format_comment_time("2026-06-18T17:15:52.31"),
            "2026-06-18T17:15:52.310"
        );
        assert_eq!(
            format_comment_time("2026-06-18T17:15:52"),
            "2026-06-18T17:15:52.000"
        );
        assert_eq!(
            format_comment_time("2026-06-18T17:15:52.3123Z"),
            "2026-06-18T17:15:52.312+00:00"
        );
    }
}