This example demonstrates the complete document read/modify/write workflow in go-docx v2.
Creates a comprehensive showcase document featuring:
- Title and subtitle formatting
- Multiple paragraph styles (Heading1, Heading2, Heading3, Quote, Normal)
- Rich text formatting (bold, italic, underline, colors, font sizes)
- Tables with merged cells and styling
- List paragraphs
- Opens an existing .docx file using
docx.OpenDocument() - Parses and reconstructs the document structure
- Provides access to all document elements
- Traverses paragraphs and tables
- Extracts text content
- Reads table dimensions (rows/columns)
- Gathers document statistics
Demonstrates two types of modifications:
- Change text color of existing runs (title → blue)
- Apply formatting to existing text (subtitle → italic)
- Update paragraph styles (Heading1 → Heading2)
- Modify text content of existing runs
- Update table cell values and formatting
- Add new paragraphs to existing documents
- Create new runs with custom formatting
- Add tables with styled cells
- Apply paragraph and character styles
- Preserve existing content and formatting
- Saves modified document with a new filename
- Preserves all original and new content
- Allows side-by-side comparison of original vs modified
cd examples/12_read_and_modify
go run main.goThis example generates two files:
12_showcase_original.docx- The original comprehensive document with all features12_modified_document.docx- The modified version with added section 5
Compare these files in Microsoft Word or LibreOffice to see the modifications clearly.
builder := docx.NewDocumentBuilder(
docx.WithTitle("Document Showcase"),
docx.WithAuthor("go-docx v2"),
)
builder.AddParagraph().
Text("Document Showcase - All Features").
Style(domain.StyleIDTitle).
End()
// ... add more content
doc, _ := builder.Build()
doc.SaveAs("12_showcase_original.docx")doc, err := docx.OpenDocument("12_showcase_original.docx")
if err != nil {
log.Fatal(err)
}paragraphs := doc.Paragraphs()
tables := doc.Tables()
fmt.Printf("Paragraphs: %d\n", len(paragraphs))
fmt.Printf("Tables: %d\n", len(tables))
for i, para := range paragraphs {
fmt.Printf("%d. %s\n", i+1, para.Text())
}// Get existing paragraphs
paragraphs := doc.Paragraphs()
// Modify title color
if len(paragraphs) > 0 {
title := paragraphs[0]
runs := title.Runs()
if len(runs) > 0 {
runs[0].SetColor(docx.Blue) // Change color
}
}
// Find and update specific paragraph
for _, para := range paragraphs {
if para.Text() == "1. Text Formatting Capabilities" {
runs := para.Runs()
if len(runs) > 0 {
runs[0].SetText("1. Text Formatting (MODIFIED)")
runs[0].SetColor(docx.Red)
}
break
}
}
// Update table cell value
tables := doc.Tables()
if len(tables) > 0 {
row, _ := tables[0].Row(2)
cell, _ := row.Cell(2)
paras := cell.Paragraphs()
if len(paras) > 0 {
runs := paras[0].Runs()
if len(runs) > 0 {
runs[0].SetText("$35.00 (UPDATED)")
runs[0].SetBold(true)
}
}
}// Add new paragraph
newPara, _ := doc.AddParagraph()
newPara.SetStyle(domain.StyleIDHeading1)
run, _ := newPara.AddRun()
run.SetText("5. Modifications (Added by Reader)")
run.SetColor(docx.Purple)
// Add new table
table, _ := doc.AddTable(3, 2)
table.SetStyle(domain.TableStyleMediumShading)
// ... populate tableerr := doc.SaveAs("12_modified_document.docx")
if err != nil {
log.Fatal(err)
}| Category | API Method | Example Usage |
|---|---|---|
| Opening | ||
| Open document | docx.OpenDocument() |
doc, err := docx.OpenDocument("file.docx") |
| Reading | ||
| Get paragraphs | doc.Paragraphs() |
paras := doc.Paragraphs() |
| Get tables | doc.Tables() |
tables := doc.Tables() |
| Get runs | para.Runs() |
runs := para.Runs() |
| Get text | para.Text() |
text := para.Text() |
| Get table row | table.Row() |
row, _ := table.Row(0) |
| Get cell | row.Cell() |
cell, _ := row.Cell(0) |
| Editing | ||
| Set text | run.SetText() |
run.SetText("New text") |
| Set color | run.SetColor() |
run.SetColor(docx.Blue) |
| Set bold | run.SetBold() |
run.SetBold(true) |
| Set italic | run.SetItalic() |
run.SetItalic(true) |
| Set style | para.SetStyle() |
para.SetStyle(domain.StyleIDHeading1) |
| Adding | ||
| Add paragraph | doc.AddParagraph() |
para, _ := doc.AddParagraph() |
| Add run | para.AddRun() |
run, _ := para.AddRun() |
| Add table | doc.AddTable() |
table, _ := doc.AddTable(3, 2) |
This pattern is useful for:
- Template Processing - Read template documents and fill in placeholders with dynamic data
- Content Updates - Update specific sections (prices, dates, names) while preserving formatting
- Document Merging - Combine multiple documents into one master document
- Automated Reports - Generate reports by modifying base documents with current data
- Batch Processing - Apply consistent changes across multiple documents
- Document Analysis - Extract and analyze content from existing documents
- Format Conversion - Read, transform, and save in different formats
- Version Updates - Update document versions by modifying specific sections
The reader infrastructure is currently 55% complete:
- ✅ Working: Paragraph reading, run reconstruction, table hydration, image reconstruction
- 🚧 In Progress: Style reading, section reading, field reading
- ⏳ Planned: Header/footer reading, advanced table features
Current known limitations:
- Complex nested tables may not fully reconstruct
- Some advanced table styles may lose fidelity
- Custom styles require full Phase 10 implementation
- Header/footer modification not yet supported
The reader aims to preserve:
- ✅ Text content
- ✅ Basic formatting (bold, italic, underline, color)
- ✅ Paragraph styles
- ✅ Table structure and content
- 🚧 Complex styling (partial)
- ⏳ Advanced features (planned)
When you run this example, you'll see:
📝 Step 1: Creating comprehensive showcase document...
✅ Created: 12_showcase_original.docx
📖 Step 2: Reading the document back...
✅ Document loaded successfully
🔍 Step 3: Inspecting document content...
📊 Document statistics:
• Paragraphs: 25
• Tables: 1
📝 First 3 paragraphs:
1. "Document Showcase - All Features"
2. "This document demonstrates all capabilities of go-docx v2"
3. ""
📋 First table:
• Rows: 4
• Columns: 3
✏️ Step 4: Modifying the document...
→ Modifying existing paragraphs...
→ Modifying existing table...
→ Adding new section...
✅ Modifications applied
💾 Step 5: Saving modified document as '12_modified_document.docx'...
✅ Saved: 12_modified_document.docx
🎉 Complete! Compare the original and modified documents to see the changes.
Generated files:
📄 12_showcase_original.docx (original showcase)
📄 12_modified_document.docx (modified version)
After running this example:
- Open both generated files in Word/LibreOffice
- Compare the original and modified versions to see:
- Title is now blue (was black)
- Subtitle is now italic (was normal)
- Section 1 heading text changed to "1. Text Formatting (MODIFIED)" in red
- Table price updated from $30.00 to $35.00 (UPDATED) in green
- New section 5 added with content summary
- Verify all original content was preserved (no data loss)
- Examine the modification summary in section 5
- See
docs/V2_DESIGN.mdfor architecture details - See
docs/V2_API_GUIDE.mdfor complete API reference - See other examples for specific feature demonstrations