### Create Tables with Styles in Go Source: https://context7.com/gomutex/godocx-examples/llms.txt Demonstrates how to create a Word document table with predefined styles, including adding header and data rows with paragraphs. This function requires the 'godocx' library. ```go package main import ( "log" "github.com/gomutex/godocx" ) func main() { document, err := godocx.NewDocument() if err != nil { log.Fatal(err) } // Create a table with a predefined style table := document.AddTable() table.Style("LightList-Accent2") // Add header row tblRow := table.AddRow() cell00 := tblRow.AddCell() cell00.AddParagraph("Column1") cell01 := tblRow.AddCell() cell01.AddParagraph("Column2") // Add data row tblRow1 := table.AddRow() cell10 := tblRow1.AddCell() cell10.AddParagraph("Row2 - Column 1") cell11 := tblRow1.AddCell() cell11.AddParagraph("Row2 - Column 2") err = document.SaveTo("table.docx") if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Create New Document with GoDocx Source: https://context7.com/gomutex/godocx-examples/llms.txt Demonstrates how to create a new, empty Word document using the godocx library and save it to a file. This is the fundamental step for all document generation tasks. ```go package main import ( "log" "github.com/gomutex/godocx" ) func main() { // Create a new document document, err := godocx.NewDocument() if err != nil { log.Fatal(err) } // Add content to the document document.AddParagraph("Hello, World") // Save the document to a file err = document.SaveTo("output.docx") if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Apply Paragraph Styles with GoDocx Source: https://context7.com/gomutex/godocx-examples/llms.txt Illustrates applying predefined styles to paragraphs, including quote styles and list styles for bulleted and numbered lists, using the godocx library. ```go package main import ( "log" "github.com/gomutex/godocx" ) func main() { document, err := godocx.NewDocument() if err != nil { log.Fatal(err) } // Apply a quote style p1 := document.AddParagraph("Intense Quote") p1.Style("IntenseQuote") // Mixed formatting in a paragraph p := document.AddParagraph("A plain paragraph having some ") p.AddText("bold").Bold(true) p.AddText(" and some ") p.AddText("italic.").Italic(true) // Bullet list bulletPara := document.AddParagraph("first item in unordered list") bulletPara.Style("List Bullet") // Numbered list numberPara := document.AddParagraph("first item in ordered list") numberPara.Style("List Number") err = document.SaveTo("styles.docx") if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Read Existing Documents in Go Source: https://context7.com/gomutex/godocx-examples/llms.txt Demonstrates how to open and read an existing Word document using the 'godocx' library. It accesses the document's body and serializes its XML structure to standard output. Requires 'godocx'. ```go package main import ( "encoding/xml" "os" "log" "github.com/gomutex/godocx" ) func main() { // Open an existing document document, err := godocx.OpenDocument("hello-world.docx") if err != nil { log.Fatal(err) } // Access document body and serialize to XML xmlEncoder := xml.NewEncoder(os.Stdout) err = document.Document.Body.MarshalXML(xmlEncoder, xml.StartElement{}) if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Create Tables from Data in Go Source: https://context7.com/gomutex/godocx-examples/llms.txt Shows how to dynamically generate a styled Word document table from structured data. It includes adding a page break before the table and populating it with records. Requires the 'godocx' library. ```go package main import ( "log" "github.com/gomutex/godocx" ) func main() { document, err := godocx.NewDocument() if err != nil { log.Fatal(err) } // Sample data records := []struct{ Qty, ID, Desc string }{ {"5", "A001", "Laptop"}, {"10", "B202", "Smartphone"}, {"2", "E505", "Smartwatch"}, } // Add page break before table document.AddPageBreak() // Create styled table table := document.AddTable() table.Style("LightList-Accent4") // Add header row hdrRow := table.AddRow() hdrRow.AddCell().AddParagraph("Qty") hdrRow.AddCell().AddParagraph("ID") hdrRow.AddCell().AddParagraph("Description") // Populate table with data for _, record := range records { row := table.AddRow() row.AddCell().AddParagraph(record.Qty) row.AddCell().AddParagraph(record.ID) row.AddCell().AddParagraph(record.Desc) } err = document.SaveTo("data-table.docx") if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Format Paragraph Text with GoDocx Source: https://context7.com/gomutex/godocx-examples/llms.txt Demonstrates adding paragraphs with various inline text formatting options such as bold, italic, color, size, strikethrough, underline, highlight, and shading using the godocx library. ```go package main import ( "log" "github.com/gomutex/godocx" "github.com/gomutex/godocx/wml/stypes" ) func main() { document, err := godocx.NewDocument() if err != nil { log.Fatal(err) } // Simple paragraph with inline formatting p := document.AddParagraph("Hello, World.") r := p.AddText(" Italic") r.Italic(true) // Empty paragraph with styled text run p1 := document.AddEmptyParagraph() r1 := p1.AddText("Hello, Parallel World") r1.Color("bc00d6") // Hex color code r1.Bold(true) // Text with various formatting effects document.AddEmptyParagraph().AddText("Strike").Strike(true) document.AddEmptyParagraph().AddText("Underline").Underline(stypes.UnderlineSingle) document.AddEmptyParagraph().AddText("Highlight").Highlight(stypes.ColorIndexBlue) document.AddEmptyParagraph().AddText("Shading").Shading(stypes.ShdSolid, "auto", "FF00A0") // Paragraph with justification jp1 := document.AddParagraph("Center Justified") jp1.Justification("center") err = document.SaveTo("paragraph.docx") if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Add Headings to Document with GoDocx Source: https://context7.com/gomutex/godocx-examples/llms.txt Shows how to add hierarchical headings to a Word document using the godocx library. Supports heading levels from 0 (title) to 4 for structuring document content. ```go package main import ( "log" "github.com/gomutex/godocx" ) func main() { document, err := godocx.NewDocument() if err != nil { log.Fatal(err) } // Level 0 is the document title document.AddHeading("Title", 0) // Levels 1-4 are standard heading levels document.AddHeading("Heading 1", 1) document.AddHeading("Heading 2", 2) document.AddHeading("Heading 3", 3) document.AddHeading("Heading 4", 4) err = document.SaveTo("headings.docx") if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Insert Images into Documents in Go Source: https://context7.com/gomutex/godocx-examples/llms.txt Illustrates inserting images into a Word document at various locations: directly, within paragraphs, and inside table cells. Requires 'godocx' and 'godocx/common/units'. Assumes 'gopher.png' exists. ```go package main import ( "log" "github.com/gomutex/godocx" "github.com/gomutex/godocx/common/units" ) func main() { document, err := godocx.NewDocument() if err != nil { log.Fatal(err) } // Add image directly to document document.AddPicture("gopher.png", units.Inch(2.9), units.Inch(2.9)) // Add image within a paragraph p := document.AddParagraph("Hello ") p.AddPicture("gopher.png", units.Inch(1), units.Inch(1)) // Add image inside a table cell table := document.AddTable() table.Style("LightList-Accent3") tblRow := table.AddRow() cell00 := tblRow.AddCell() cell00.AddParagraph("Column1") cell01 := tblRow.AddCell() cell01.AddParagraph("Column2") tblRow1 := table.AddRow() cell10 := tblRow1.AddCell() cell10.AddParagraph("Row2 - Column 1") cell11 := tblRow1.AddCell() p2 := cell11.AddEmptyPara() p2.AddPicture("gopher.png", units.Inch(1), units.Inch(1)) err = document.SaveTo("pic.docx") if err != nil { log.Fatal(err) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.