Copy part contents to a part in another package

This example copies the raw data from the theme part in one WordprocessingML package to the theme part in another package.

Theme parts are optional. The example returns Ok(None) when either package does not have a theme part.

Theme parts

A Theme part stores a document theme: color scheme, font scheme, and effect scheme. In WordprocessingML, a theme affects heading colors and styles; in SpreadsheetML, it affects cell and chart formatting; in PresentationML, it affects slides, handouts, notes, and masters.

WordprocessingML and SpreadsheetML packages can have zero or one Theme part associated with the main document or workbook part. PresentationML packages can have Theme parts associated with the presentation, slide masters, notes masters, or handout masters.

Copy the theme part

#![allow(unused)]
fn main() {
pub fn copy_theme_part(
  source_path: &Path,
  target_path: &Path,
) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> {
  let settings = OpenSettings {
    open_mode: PackageOpenMode::Lazy,
    ..Default::default()
  };
  let source = WordprocessingDocument::new_from_file_with_settings(source_path, settings)?;
  let mut target = WordprocessingDocument::new_from_file_with_settings(target_path, settings)?;

  let source_main = source.main_document_part()?;
  let target_main = target.main_document_part()?;
  let Some(source_theme) = source_main.theme_part(&source) else {
    return Ok(None);
  };
  let Some(target_theme) = target_main.theme_part(&target) else {
    return Ok(None);
  };

  let theme_data = source_theme.data_to_vec(&source).unwrap_or_default();
  target_theme.set_data(&mut target, theme_data)?;

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

The function opens the source package and target package, finds each main document part, looks up each theme part, copies the source bytes, and saves the updated target package to memory.

This is a raw part-data copy. It does not parse or validate the theme XML.

When to copy raw part data

Raw part copying is useful when:

  • You want to preserve a part exactly as stored.
  • You do not need to inspect or modify the XML structure.
  • The source and target part types are the same.

Use generated schema root elements instead when you need to read or modify specific XML elements or attributes.

Before running this workflow on real files, make sure the source document actually has a Theme part. If the target lacks one, create it first, as shown in Replace the theme part in a word processing document.