Read, modify, and save existing DOCX files with perfect round-trip fidelity.

1. Reading a Document

Load an existing DOCX file using DocumentFactory:

doc = Uniword::DocumentFactory.from_file('input.docx')

# Extract all text
puts doc.text

# Iterate paragraphs
doc.paragraphs.each { |p| puts p.text }

Alternatively, use the Document.open method:

doc = Uniword::Document.open('report.docx')

2. Reading Document Structure

Access the underlying OOXML structure through the document model:

doc = Uniword::Document.open('report.docx')

# Access paragraphs with formatting
doc.paragraphs.each do |para|
  puts "Text: #{para.text}"
  puts "Style: #{para.style}" if para.respond_to?(:style)
end

# Access tables
doc.tables.each do |table|
  table.rows.each do |row|
    row.cells.each do |cell|
      puts cell.text
    end
  end
end

3. Modifying a Document

After loading, modify the document as needed:

doc = Uniword::Document.open('input.docx')

# Add new content
doc.add_paragraph("Added by Uniword", bold: true)

# Append a table
table = doc.add_table(2, 3)
table.rows[0].cells[0].add_paragraph("Header 1")

# Apply a theme
doc.apply_theme('celestial')

# Save the modified document
doc.save('modified.docx')

4. Using Docx::Package Directly

For lower-level access, use Docx::Package which models the entire DOCX ZIP structure:

package = Uniword::Docx::Package.from_file('input.docx')

# Access individual parts
puts package.core_properties.title
puts package.app_properties.application
puts package.theme.name

# Save back to file
package.to_file('output.docx')

5. Round-Trip Guarantee

Uniword guarantees round-trip fidelity: load a DOCX, modify it, save it, and all original content is preserved plus your modifications are applied.

original = Uniword::Document.open('complex.docx')
original.add_paragraph("New content")
original.save('modified.docx')
# modified.docx has ALL original content + new paragraph