docx

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateMinimalDocx

func CreateMinimalDocx(outputPath string) error

CreateMinimalDocx creates a minimal valid .docx file for testing

Types

type Body

type Body struct {
	XMLName    xml.Name    `xml:"body"`
	Paragraphs []Paragraph `xml:"p"`
	Tables     []Table     `xml:"tbl"`
}

Body represents the document body

type Bold

type Bold struct {
	XMLName xml.Name `xml:"b"`
}

Bold represents bold formatting

type Break

type Break struct {
	XMLName xml.Name `xml:"br"`
}

Break represents a line break

type Color

type Color struct {
	XMLName xml.Name `xml:"color"`
	Val     string   `xml:"val,attr"`
}

Color represents text color

type ContentTypes

type ContentTypes struct {
	XMLName xml.Name `xml:"Types"`
}

ContentTypes represents [Content_Types].xml

type Document

type Document struct {
	FilePath     string
	Body         *Body
	Styles       *Styles
	ContentTypes *ContentTypes
	Rels         *Relationships
	// contains filtered or unexported fields
}

Document represents a .docx document structure

func CreateFromTemplate

func CreateFromTemplate(templatePath string) (*Document, error)

CreateFromTemplate creates a new document based on a template

func New

func New() *Document

New creates a new empty document

func Open

func Open(filePath string) (*Document, error)

Open opens and reads a .docx file

func ReadBytes

func ReadBytes(data []byte) (*Document, error)

ReadBytes reads a .docx file from bytes

func ReadFrom

func ReadFrom(r io.Reader) (*Document, error)

ReadFrom reads a .docx document from an io.Reader

func (*Document) AddParagraph

func (d *Document) AddParagraph(text string, opts ...ParagraphOption)

AddParagraph adds a new paragraph to the document

func (*Document) AddParagraphAt

func (d *Document) AddParagraphAt(index int, text string, opts ...ParagraphOption) error

AddParagraphAt inserts a paragraph at a specific index

func (*Document) AddTable

func (d *Document) AddTable(rows, cols int) *Table

AddTable adds a new table to the document

func (*Document) Clear

func (d *Document) Clear()

Clear removes all paragraphs and tables from the document

func (*Document) Clone

func (d *Document) Clone() *Document

Clone creates a deep copy of the document

func (*Document) DeleteParagraph

func (d *Document) DeleteParagraph(index int) error

DeleteParagraph removes a paragraph by index

func (*Document) DeleteParagraphsRange

func (d *Document) DeleteParagraphsRange(start, end int) error

DeleteParagraphsRange deletes multiple paragraphs from start to end (inclusive)

func (*Document) DeleteTable

func (d *Document) DeleteTable(index int) error

DeleteTable removes a table by index

func (*Document) FindText

func (d *Document) FindText(searchText string) []int

FindText searches for text in the document and returns paragraph indices

func (*Document) GetParagraphCount

func (d *Document) GetParagraphCount() int

GetParagraphCount returns the number of paragraphs

func (*Document) GetParagraphText

func (d *Document) GetParagraphText(index int) (string, error)

GetParagraphText returns text from a specific paragraph

func (*Document) GetTableCount

func (d *Document) GetTableCount() int

GetTableCount returns the number of tables

func (*Document) GetText

func (d *Document) GetText() string

GetText extracts all text from the document

func (*Document) ReplaceText

func (d *Document) ReplaceText(oldText, newText string) int

ReplaceText replaces all occurrences of old text with new text

func (*Document) ReplaceTextInParagraph

func (d *Document) ReplaceTextInParagraph(index int, oldText, newText string) (int, error)

ReplaceTextInParagraph replaces text in a specific paragraph

func (*Document) Save

func (d *Document) Save(filePath string) error

Save saves the document to a file

func (*Document) SaveAs

func (d *Document) SaveAs(filePath string) error

SaveAs saves the document to a new file

func (*Document) ToBytes

func (d *Document) ToBytes() ([]byte, error)

ToBytes returns the document as bytes

func (*Document) WriteToZip

func (d *Document) WriteToZip(w *zip.Writer) error

WriteToZip writes the document to an open zip.Writer (useful for streaming)

type Italic

type Italic struct {
	XMLName xml.Name `xml:"i"`
}

Italic represents italic formatting

type Jc

type Jc struct {
	XMLName xml.Name `xml:"jc"`
	Val     string   `xml:"val,attr"` // left, center, right, both
}

Jc represents text justification

type PProps

type PProps struct {
	XMLName xml.Name `xml:"pPr"`
	Style   *PStyle  `xml:"pStyle,omitempty"`
	Jc      *Jc      `xml:"jc,omitempty"` // Justification
	Spacing *Spacing `xml:"spacing,omitempty"`
}

PProps represents paragraph properties

type PStyle

type PStyle struct {
	XMLName xml.Name `xml:"pStyle"`
	Val     string   `xml:"val,attr"`
}

PStyle represents paragraph style

type Paragraph

type Paragraph struct {
	XMLName xml.Name `xml:"p"`
	Runs    []Run    `xml:"r"`
	Props   *PProps  `xml:"pPr,omitempty"`
}

Paragraph represents a paragraph in the document

type ParagraphOption

type ParagraphOption func(*Paragraph)

ParagraphOption is a function type for configuring paragraphs

func WithAlignment

func WithAlignment(align string) ParagraphOption

WithAlignment sets paragraph alignment ("left", "center", "right", "both")

func WithBold

func WithBold() ParagraphOption

WithBold makes the paragraph text bold

func WithColor

func WithColor(color string) ParagraphOption

WithColor sets the text color (hex without #, e.g., "FF0000" for red)

func WithItalic

func WithItalic() ParagraphOption

WithItalic makes the paragraph text italic

func WithSize

func WithSize(size string) ParagraphOption

WithSize sets the font size (in half-points, e.g., 24 = 12pt)

func WithStyle

func WithStyle(styleName string) ParagraphOption

WithStyle sets a paragraph style

type RProps

type RProps struct {
	XMLName xml.Name `xml:"rPr"`
	Bold    *Bold    `xml:"b,omitempty"`
	Italic  *Italic  `xml:"i,omitempty"`
	Size    *Size    `xml:"sz,omitempty"`
	Color   *Color   `xml:"color,omitempty"`
}

RProps represents run properties

type Relationships

type Relationships struct {
	XMLName xml.Name `xml:"Relationships"`
}

Relationships represents document relationships

type Run

type Run struct {
	XMLName xml.Name `xml:"r"`
	Props   *RProps  `xml:"rPr,omitempty"`
	Text    []Text   `xml:"t"`
	Tab     *Tab     `xml:"tab,omitempty"`
	Break   *Break   `xml:"br,omitempty"`
}

Run represents a text run

type Size

type Size struct {
	XMLName xml.Name `xml:"sz"`
	Val     string   `xml:"val,attr"`
}

Size represents font size

type Spacing

type Spacing struct {
	XMLName xml.Name `xml:"spacing"`
	Before  string   `xml:"before,attr,omitempty"`
	After   string   `xml:"after,attr,omitempty"`
	Line    string   `xml:"line,attr,omitempty"`
}

Spacing represents paragraph spacing

type Styles

type Styles struct {
	XMLName xml.Name `xml:"styles"`
}

Styles represents document styles

type Tab

type Tab struct {
	XMLName xml.Name `xml:"tab"`
}

Tab represents a tab character

type Table

type Table struct {
	XMLName xml.Name `xml:"tbl"`
	Props   *TblPr   `xml:"tblPr,omitempty"`
	Grid    *TblGrid `xml:"tblGrid,omitempty"`
	Rows    []TblRow `xml:"tr"`
}

Table represents a table in the document

func (*Table) AddRow

func (t *Table) AddRow()

AddRow adds a new row to the table

func (*Table) DeleteRow

func (t *Table) DeleteRow(index int) error

DeleteRow deletes a row from the table

func (*Table) GetCellText

func (t *Table) GetCellText(row, col int) (string, error)

GetCellText gets the text content of a cell

func (*Table) GetColumnCount

func (t *Table) GetColumnCount() int

GetColumnCount returns the number of columns in the table

func (*Table) GetRowCount

func (t *Table) GetRowCount() int

GetRowCount returns the number of rows in the table

func (*Table) SetCellText

func (t *Table) SetCellText(row, col int, text string) error

SetCellText sets the text content of a cell

type TblCell

type TblCell struct {
	XMLName xml.Name    `xml:"tc"`
	Props   *TcPr       `xml:"tcPr,omitempty"`
	Content []Paragraph `xml:"p"`
}

TblCell represents a table cell

type TblGrid

type TblGrid struct {
	XMLName xml.Name     `xml:"tblGrid"`
	Cols    []TblGridCol `xml:"gridCol"`
}

TblGrid represents table grid/columns

type TblGridCol

type TblGridCol struct {
	XMLName xml.Name `xml:"gridCol"`
	W       string   `xml:"w,attr,omitempty"`
}

TblGridCol represents a table column

type TblPr

type TblPr struct {
	XMLName xml.Name  `xml:"tblPr"`
	Style   *TblStyle `xml:"tblStyle,omitempty"`
	Width   *TblWidth `xml:"tblW,omitempty"`
}

TblPr represents table properties

type TblRow

type TblRow struct {
	XMLName xml.Name  `xml:"tr"`
	Props   *TrPr     `xml:"trPr,omitempty"`
	Cells   []TblCell `xml:"tc"`
}

TblRow represents a table row

type TblStyle

type TblStyle struct {
	XMLName xml.Name `xml:"tblStyle"`
	Val     string   `xml:"val,attr"`
}

TblStyle represents table style

type TblWidth

type TblWidth struct {
	XMLName xml.Name `xml:"tblW"`
	Type    string   `xml:"type,attr"`
	W       string   `xml:"w,attr"`
}

TblWidth represents table width

type TcPr

type TcPr struct {
	XMLName xml.Name  `xml:"tcPr"`
	Width   *TblWidth `xml:"tcW,omitempty"`
}

TcPr represents cell properties

type Text

type Text struct {
	XMLName xml.Name `xml:"t"`
	Space   string   `xml:"space,attr,omitempty"`
	Content string   `xml:",chardata"`
}

Text represents text content

type TrPr

type TrPr struct {
	XMLName xml.Name `xml:"trPr"`
}

TrPr represents row properties

Jump to

Keyboard shortcuts

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