Skip to content

Quick Start ​

This page builds one .docx that exercises the four features most teams touch on day one: a native Office Math formula, a DrawingML shape, a two-column section, and Save.

The public API keeps PHPWord names (AddSection, AddText, IOFactory) with idiomatic Go types and error returns.

New / AddSection / Save ​

Signature ​

go
func New() *Document
func (d *Document) AddSection(style ...any) *element.Section
func (d *Document) Save(filename string) error

Parameters ​

NameTypeDescription
style...anyOptional style.Section (page size, margins, orientation, break type).
filenamestringDestination path. Parent directories must already exist.

Notes ​

  • New allocates an empty document with Calibri 11 pt as the default run font.
  • AddSection appends a w:sectPr block. Body elements attach to the returned *element.Section.
  • Save is CreateWriter(doc, "Word2007") plus a file. Relationship IDs (rIdN) and media names (word/media/imageN) are assigned at write time.

Complete example ​

Save as main.go and run go run .. Microsoft Word opens hello.docx without a repair dialog.

go
package main

import (
	"log"

	"github.com/yunkeweb/go-word"
	"github.com/yunkeweb/go-word/style"
)

func main() {
	doc := word.New()
	doc.SetDefaultFontName("Calibri")
	doc.SetDefaultAsianFontName("Microsoft YaHei")
	doc.SetDefaultFontSize(11)

	info := doc.GetDocInfo()
	info.Title = "GoWord v0.13.0"
	info.Creator = "GoWord"

	sec := doc.AddSection()
	sec.AddTitle("GoWord v0.13.0", 1)
	sec.AddText("Native Office Math:")
	sec.AddMath(`\frac{a}{b}`)

	p := sec.AddTextRun()
	p.AddText("Pythagoras: ")
	p.AddMath(`x^{2} + y^{2} = z^{2}`)

	doc.AddShape(word.ShapeRoundRect, word.ShapeOptions{
		FillColor: "5B9BD5",
		LineColor: "2E75B6",
		Text:      "DrawingML",
		Font:      style.Font{Bold: true, Color: "FFFFFF"},
	})

	cols := doc.AddSection()
	cols.SetColumns(2, 720, true)
	cols.AddText("The left column starts here. Word flows this section into two equal columns.")
	cols.AddText("A separator line is emitted as w:cols w:sep.")

	if err := doc.Save("hello.docx"); err != nil {
		log.Fatal(err)
	}
}

What Word shows ​

FeatureOn screenOpenXML
Display formulaA centered equation you can double-clickw:p / m:oMathPara / m:oMath / m:f
Inline formulaPythagoras sits in the same line as the labelm:oMath beside w:r
Rounded rectangleBlue pill with white “DrawingML”wps:wsp / a:prstGeom prst="roundRect"
Two columnsEqual columns with a vertical separatorw:cols w:num="2" w:space="720" w:sep="1"

IOFactory aliases ​

Signature ​

go
func CreateWriter(doc *Document, name string) (Writer, error)
func CreateReader(name string) (Reader, error)
func Load(filename string, readerName ...string) (*Document, error)
func Open(filePath string) (*Document, error)
func LoadWithOptions(filename string, opts ReadOptions) (*Document, error)
func OpenWithOptions(filePath string, opts ReadOptions) (*Document, error)
func ReadWithOptions(r io.Reader, opts ReadOptions) (*Document, error)
func LoadBytesWithOptions(data []byte, opts ReadOptions) (*Document, error)

name is "Word2007" (the default). Load / Open build a full DOM; for O(1) text extraction see Streaming Parser.

Use the WithOptions variants for untrusted ZIP input. ReadOptions caps the compressed archive, each uncompressed member, the declared package total, and the number of entries. Zero disables a limit; negative fields are rejected before I/O. Exceeding a limit returns ErrReadLimitExceeded.

MaxArchiveSize and MaxPartSize are byte limits. MaxTotalSize is the sum of declared uncompressed ZIP members, and MaxEntries counts ZIP members. The options apply to one call and do not change legacy unbounded functions. Template input has matching constructors:

go
func NewTemplateProcessorWithOptions(filename string, opts ReadOptions) (*TemplateProcessor, error)
func NewTemplateProcessorBytesWithOptions(data []byte, opts ReadOptions) (*TemplateProcessor, error)

Next ​

TopicPage
How the ZIP is assembledArchitecture
Paragraphs, tables, imagesParagraphs & Runs
SDT form controlsStructured Document Tags
Repeating headers / cantSplitTables
Tiled watermark and AllowEditWatermark & Protection
LaTeX → Word equationsOffice Math
ChartsDrawingML Charts

Released under the GNU LGPL v3.0.