Change text in a table in a word processing document

Table cell text is stored in paragraphs inside <w:tc/> cells. Changing table text requires locating the correct table, row, cell, paragraph, and run.

Tables are block-level content in the document body. A table (tbl) contains table properties, an optional table grid, rows (tr), cells (tc), and block-level content inside each cell. Even an otherwise empty cell normally contains a paragraph.

Table structure

<w:tbl>
  <w:tblPr>
    <w:tblW w:w="5000" w:type="pct"/>
    <w:tblBorders>
      <w:top w:val="single" w:sz="4"/>
      <w:left w:val="single" w:sz="4"/>
      <w:bottom w:val="single" w:sz="4"/>
      <w:right w:val="single" w:sz="4"/>
    </w:tblBorders>
  </w:tblPr>
  <w:tblGrid>
    <w:gridCol w:w="10296"/>
  </w:tblGrid>
  <w:tr>
    <w:tc>
      <w:tcPr><w:tcW w:w="0" w:type="auto"/></w:tcPr>
      <w:p><w:r><w:t>Cell text</w:t></w:r></w:p>
    </w:tc>
  </w:tr>
</w:tbl>

Rust workflow

#![allow(unused)]
fn main() {
pub fn change_text_in_first_table_cell(
  path: &Path,
  new_text: &str,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
  let mut document = WordprocessingDocument::new_from_file_with_settings(path, lazy_settings())?;
  let main_part = document.main_document_part()?;
  let xml = main_part.data_as_str(&document)?.unwrap_or_default();
  let updated_xml = replace_first_table_cell_text(xml, new_text)?;

  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 listing scopes the edit to the first table and replaces the first text element in that table. Avoid broad string replacement across document.xml; parse table boundaries and update only the target cell content.

The upstream sample targets the first table, second row, and third cell, then replaces text in the first run of the first paragraph. A production Rust API should make table, row, and cell selection explicit, handle missing rows or cells as errors, and decide whether replacing text should preserve existing runs or rebuild the cell paragraph.

In ooxmlsdk, generated schema types include Table, TableProperties, TableGrid, GridColumn, TableRow, TableCell, TableCellProperties, Paragraph, Run, and Text.