Retrieve the number of slides in a presentation document

This example counts slide parts in a PresentationML package. It can include all slides or skip slides marked hidden with show="0" or show="false" in the slide XML.

Count slides

#![allow(unused)]
fn main() {
pub fn count_slides(
  path: &Path,
  include_hidden: bool,
) -> Result<usize, Box<dyn std::error::Error>> {
  let document = PresentationDocument::new_from_file_with_settings(path, lazy_settings())?;
  let presentation_part = document.presentation_part()?;

  if include_hidden {
    return Ok(presentation_part.slide_parts(&document).count());
  }

  let mut count = 0;
  let slide_parts: Vec<_> = presentation_part.slide_parts(&document).collect();
  for slide_part in slide_parts {
    let xml = slide_part.data_as_str(&document)?.unwrap_or_default();
    if !xml.contains(r#"show="0""#) && !xml.contains(r#"show="false""#) {
      count += 1;
    }
  }
  Ok(count)
}
}

The function opens the package lazily, gets the presentation part, and iterates presentation_part.slide_parts(&document).

When include_hidden is true, the function returns the number of slide parts. When include_hidden is false, it reads each slide part as XML and skips slides whose root has a hidden show value.

Notes

This example uses raw slide XML to check the show attribute. That keeps the example focused on package navigation and avoids loading every slide root. If you are already parsing slide roots for other edits, use the generated slide schema type instead.

In PresentationML, a missing show value means the slide is visible. A value of true also means visible; hidden slides are represented by a false value, commonly serialized as show="0" or show="false".