Table of Contents
Uniword supports the full document lifecycle: create, load, modify, and save DOCX and MHTML files.
1. Creating documents
Use Document.new to create a blank document. Add content with convenience
methods, then save.
require "uniword"
doc = Uniword::Document.new
doc.add_paragraph("Hello World", bold: true)
doc.save("output.docx")
You can also create paragraphs and runs directly:
doc = Uniword::Document.new
para = Uniword::Paragraph.new
para.add_text("Bold", bold: true)
para.add_text(" Italic", italic: true)
doc.add_element(para)
doc.save("output.docx")
2. Loading documents
DocumentFactory.from_file opens an existing DOCX or MHTML file. The format
is auto-detected from the file extension.
# Auto-detect format from extension
doc = Uniword::DocumentFactory.from_file("input.docx")
# Explicit format
doc = Uniword::DocumentFactory.from_file("input.doc", format: :mhtml)
3. Reading content
Once loaded, access document content through convenience methods:
doc = Uniword::DocumentFactory.from_file("report.docx")
# Full text content
puts doc.text
# Iterate paragraphs
doc.paragraphs.each { |p| puts p.text }
# Count tables
puts "Tables: #{doc.tables.count}"
4. Modifying documents
Loaded documents can be modified and saved back:
doc = Uniword::DocumentFactory.from_file("template.docx")
# Append content
doc.add_paragraph("Appended paragraph")
# Insert at the beginning
doc.paragraphs.first.add_text("Prefix: ", bold: true)
# Save changes
doc.save("modified.docx")
5. Saving documents
save writes the document to disk. The output format is auto-detected from
the file extension.
# Save as DOCX (default)
doc.save("output.docx")
# Save as MHTML
doc.save("output.doc")
# Explicit format
doc.save("output.mht", format: :mhtml)
6. Format conversion
Because format detection is automatic, converting between DOCX and MHTML is a load-then-save operation:
# DOCX to MHTML
doc = Uniword::DocumentFactory.from_file("input.docx")
doc.save("output.doc")
# MHTML to DOCX
doc = Uniword::DocumentFactory.from_file("input.doc")
doc.save("output.docx")
7. Error handling
Document operations raise typed exceptions that you can rescue individually:
begin
doc = Uniword::DocumentFactory.from_file("missing.docx")
rescue Uniword::FileNotFoundError => e
puts "File not found: #{e.path}"
rescue Uniword::CorruptedFileError => e
puts "Corrupted file: #{e.reason}"
rescue Uniword::InvalidFormatError => e
puts "Invalid format: #{e.message}"
rescue Uniword::Error => e
puts "Error: #{e.message}"
end