Change the fill color of a shape in a presentation

Shape fill color is stored in the slide XML, usually under a shape's <p:spPr/> properties. A solid fill uses DrawingML color markup such as <a:solidFill/>.

Shape fill markup

<p:sp>
  <p:nvSpPr>
    <p:cNvPr id="2" name="Accent shape"/>
  </p:nvSpPr>
  <p:spPr>
    <a:solidFill>
      <a:srgbClr val="FF0000"/>
    </a:solidFill>
  </p:spPr>
</p:sp>

The val attribute stores the RGB color as six hexadecimal digits.

Shape tree

Slide content lives under the shape tree (p:spTree). It contains the non-visual group properties, group shape properties, and then zero or more drawing objects:

ElementMeaning
p:spShape
p:grpSpGroup shape
p:graphicFrameGraphic frame
p:cxnSpConnection shape
p:picPicture
p:extLstExtension list

The upstream sample changes the first shape on the first slide, so the test file must contain at least one shape. A production writer should select by a stable shape ID or name instead.

Rust workflow

#![allow(unused)]
fn main() {
pub fn change_first_shape_fill_color(
  path: &Path,
  slide_index: usize,
  rgb_hex: &str,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
  let mut document = PresentationDocument::new_from_file_with_settings(path, lazy_settings())?;
  let presentation_part = document.presentation_part()?;
  let Some(slide_part) = presentation_part.slide_parts(&document).nth(slide_index) else {
    return Err(format!("slide index {slide_index} not found").into());
  };
  let xml = slide_part.data_as_str(&document)?.unwrap_or_default();
  let updated_xml = set_first_shape_solid_fill(xml, rgb_hex)?;

  slide_part.set_data(&mut document, updated_xml.into_bytes())?;

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

The listing updates the first shape on the selected slide. For a broader writer, do not use broad text replacement across the whole slide. Parse the slide XML, locate the intended shape by id or name, update only its fill subtree, and then write the part back through the package.