Create a presentation document by providing a file name

Creating a .pptx from scratch requires more than writing ppt/presentation.xml. A valid package also needs content type declarations, package relationships, presentation relationships, slide parts, and any required masters or layouts.

In ooxmlsdk, create the package with PresentationDocument::create(PresentationDocumentType::Presentation), add the presentation part, add slide parts from the presentation part, write the root XML, and save the package to the file or writer your application owns.

Minimal package pieces

A minimal presentation package includes:

  • [Content_Types].xml,
  • _rels/.rels pointing to ppt/presentation.xml,
  • ppt/presentation.xml,
  • ppt/_rels/presentation.xml.rels,
  • one or more ppt/slides/slideN.xml parts.

This minimal writer creates a package with one slide relationship in memory:

#![allow(unused)]
fn main() {
pub fn create_presentation_document() -> Result<Vec<u8>, Box<dyn std::error::Error>> {
  let mut document = PresentationDocument::create(PresentationDocumentType::Presentation);
  let presentation_part = document.add_new_part_auto_id::<PresentationPart>()?;
  let slide_part = presentation_part.add_new_part_auto_id::<_, SlidePart>(&mut document)?;
  let slide_relationship_id = presentation_part
    .get_id_of_part(&document, &slide_part)
    .expect("slide relationship id")
    .to_string();

  presentation_part.set_data(
    &mut document,
    format!(
      r#"<p:presentation xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:sldIdLst><p:sldId id="256" r:id="{slide_relationship_id}"/></p:sldIdLst><p:sldSz cx="12192000" cy="6858000"/><p:notesSz cx="6858000" cy="9144000"/></p:presentation>"#
    )
    .into_bytes(),
  )?;
  slide_part.set_data(
    &mut document,
    br#"<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><p:cSld><p:spTree><p:nvGrpSpPr><p:cNvPr id="1" name=""/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr><p:grpSpPr/></p:spTree></p:cSld></p:sld>"#.to_vec(),
  )?;

  let mut buffer = Cursor::new(Vec::new());
  document.save(&mut buffer)?;
  Ok(buffer.into_inner())
}
}

PresentationML roots

The main presentation part has a single p:presentation root. A usable presentation normally needs related slide, slide master, slide layout, and theme parts. The corresponding generated Rust schema types are:

PresentationML elementRust type
p:presentationooxmlsdk::schemas::p::Presentation
p:sldooxmlsdk::schemas::p::Slide
p:sldMasterooxmlsdk::schemas::p::SlideMaster
p:sldLayoutooxmlsdk::schemas::p::SlideLayout
a:themeooxmlsdk::schemas::a::Theme

The p:presentation root usually references slide masters, notes masters, handout masters, and slides by relationship IDs. Slide IDs are stored in p:sldIdLst; the relationship ID points to the slide part, while the numeric slide ID is part of the presentation markup.

Production checks

A production presentation writer should still validate:

  • package and presentation relationships,
  • content type overrides,
  • slide id ordering,
  • slide master and layout references when required,
  • PowerPoint compatibility after save.

For template workflows, PresentationDocument::create_from_template opens a .potx or .potm as an editable presentation package. Use document_type() to inspect the current type and change_document_type(...) when converting the package content type deliberately.