Search and replace text in a document part

This example performs a simple string replacement against the raw XML of the main document part.

This approach is intentionally limited. It can be useful for diagnostics or controlled fixtures, but it is not a robust WordprocessingML editing strategy. A search string can cross run boundaries, appear in attributes, or accidentally match XML markup.

Simple raw XML replacement

#![allow(unused)]
fn main() {
pub fn search_and_replace_main_document(
  path: &Path,
  search: &str,
  replacement: &str,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
  let settings = OpenSettings {
    open_mode: PackageOpenMode::Lazy,
    ..Default::default()
  };
  let mut document = WordprocessingDocument::new_from_file_with_settings(path, settings)?;
  let main_part = document.main_document_part()?;
  let xml = main_part.data_as_str(&document)?.unwrap_or_default();
  let updated_xml = xml.replace(search, replacement);

  main_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 function:

  1. Opens a .docx.
  2. Reads the main document part as UTF-8 XML.
  3. Applies str::replace.
  4. Writes the updated XML bytes back to the same part.
  5. Saves the package to memory.

Safer approaches

For production document editing, prefer schema-aware traversal and updates where possible. Word text may be split across multiple runs (w:r) and text nodes (w:t), so a naive string replacement can miss visible text or corrupt the part XML.

Use this raw replacement pattern only when you control the input shape.