docx

package
v0.1.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2026 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package docx builds a minimal but complete .docx using only the Go standard library (archive/zip + encoding/xml + image).

Supported OOXML parts:

[Content_Types].xml
_rels/.rels
docProps/core.xml
word/document.xml
word/styles.xml
word/settings.xml
word/numbering.xml
word/footnotes.xml      (when footnotes are present)
word/media/image*.ext   (when images are present)
word/_rels/document.xml.rels

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Document

type Document struct {
	// contains filtered or unexported fields
}

Document accumulates content and serialises it to a .docx file.

func LoadTemplate

func LoadTemplate(path string) (*Document, error)

LoadTemplate opens a reference .docx and borrows its styles.xml.

func New

func New() *Document

New returns an empty Document.

func (*Document) AddFootnote

func (d *Document) AddFootnote() (int, *Para)

AddFootnote registers a new footnote and returns its 1-based id and a *Para into which the caller writes the formatted footnote content (runs, hyperlinks, etc.).

func (*Document) AddParagraph

func (d *Document) AddParagraph(style string) *Para

AddParagraph appends a new paragraph with the given Word style name.

func (*Document) AddTable

func (d *Document) AddTable() *Table

AddTable appends a new table.

func (*Document) BulletNumID

func (d *Document) BulletNumID() int

BulletNumID returns the fixed numId for unordered lists.

func (*Document) NewOrderedNumID

func (d *Document) NewOrderedNumID() int

NewOrderedNumID allocates a new numId for an ordered list instance. Each separate ordered list must get its own numId so numbering restarts.

func (d *Document) RegisterHyperlink(url string) string

RegisterHyperlink returns a stable rId for the given URL, creating one if needed.

func (*Document) RegisterImage

func (d *Document) RegisterImage(path string) (string, int64, int64, error)

RegisterImage reads the image file at path, stores its bytes, and returns (rId, widthEMU, heightEMU). On error the image is skipped and ("", 0, 0, err) is returned so the caller can fall back to alt-text.

func (*Document) SaveToFile

func (d *Document) SaveToFile(path string) error

SaveToFile writes the complete .docx to path.

func (*Document) Write

func (d *Document) Write(w io.Writer) (retErr error)

Write serialises the .docx to w.

type Para

type Para struct {
	// contains filtered or unexported fields
}

Para is a paragraph under construction.

func (*Para) AddBoldItalicRun

func (p *Para) AddBoldItalicRun(text string)

AddBoldItalicRun appends a bold+italic run.

func (*Para) AddBoldRun

func (p *Para) AddBoldRun(text string)

AddBoldRun appends a bold run.

func (*Para) AddBreak

func (p *Para) AddBreak()

AddBreak adds a hard line-break to the most recent run.

func (*Para) AddCodeColorRun

func (p *Para) AddCodeColorRun(text, color string)

AddCodeColorRun appends a Courier New run with a colour (syntax highlighting).

func (*Para) AddCodeRun

func (p *Para) AddCodeRun(text string)

AddCodeRun appends a Courier New run (inline code).

func (*Para) AddColorRun

func (p *Para) AddColorRun(text, color string)

AddColorRun appends a coloured run (non-monospace).

func (*Para) AddFootnoteRef

func (p *Para) AddFootnoteRef(id int)

AddFootnoteRef inserts an inline footnote reference mark.

func (*Para) AddFormattedRun

func (p *Para) AddFormattedRun(text string, bold, italic, strike bool)

AddFormattedRun appends a run with explicit bold/italic/strike flags. This is the general form; the typed helpers above are convenience wrappers.

func (*Para) AddHyperlinkRun

func (p *Para) AddHyperlinkRun(text, rId string, bold, italic, strike bool)

AddHyperlinkRun appends a hyperlink run. bold/italic/strike carry through from the surrounding emphasis context.

func (*Para) AddImageItem

func (p *Para) AddImageItem(rId, altText string, widthEMU, heightEMU int64)

AddImageItem embeds an image run.

func (*Para) AddItalicRun

func (p *Para) AddItalicRun(text string)

AddItalicRun appends an italic run.

func (*Para) AddRun

func (p *Para) AddRun(text string)

AddRun appends a plain run.

func (*Para) AddStrikeRun

func (p *Para) AddStrikeRun(text string)

AddStrikeRun appends a struck-through run.

func (*Para) ForceBold

func (p *Para) ForceBold()

ForceBold makes every run in this paragraph bold regardless of run props.

func (*Para) SetBlockquote

func (p *Para) SetBlockquote()

SetBlockquote adds a left border to the paragraph (depth 1).

func (*Para) SetBlockquoteDepth

func (p *Para) SetBlockquoteDepth(depth int)

SetBlockquoteDepth sets the blockquote nesting depth (1-based). Each additional level adds more left indentation.

func (*Para) SetHRule

func (p *Para) SetHRule()

SetHRule adds a bottom border to the paragraph (thematic break).

func (*Para) SetNumPr

func (p *Para) SetNumPr(numId, ilvl int)

SetNumPr marks the paragraph as a list item.

type Row

type Row struct {
	// contains filtered or unexported fields
}

Row is a table row.

func (*Row) AddCell

func (r *Row) AddCell(style string) *Para

AddCell appends a cell and returns its embedded paragraph. Header rows automatically force-bold their cell paragraph.

type Table

type Table struct {
	// contains filtered or unexported fields
}

Table is a table under construction.

func (*Table) AddRow

func (t *Table) AddRow(isHeader bool) *Row

AddRow appends a row. isHeader=true marks it as a header row (shaded + bold).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL