docx

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTemplateError added in v0.0.5

func NewTemplateError(op string, err error) error

NewTemplateError creates a new template error with context

func PreprocessTemplate added in v0.0.5

func PreprocessTemplate(content string, data map[string]any) (string, error)

PreprocessTemplate processes template directives (conditionals and loops) in the document content This should be called before regular placeholder replacement

Types

type ChartData added in v0.0.5

type ChartData struct {
	Categories []string
	Series     []ChartSeries
}

ChartData represents data for a chart

type ChartSeries added in v0.0.5

type ChartSeries struct {
	Name   string
	Values []float64
}

ChartSeries represents a data series in a chart

type Metadata added in v0.0.5

type Metadata struct {
	Title       string
	Subject     string
	Author      string
	Keywords    string
	Description string
	Category    string
}

Metadata represents document metadata properties

type PageSetup added in v0.0.5

type PageSetup struct {
	PageWidth    int // in twips (1/1440 inch)
	PageHeight   int
	MarginTop    int
	MarginBottom int
	MarginLeft   int
	MarginRight  int
	Orientation  string // "portrait" or "landscape"
}

PageSetup represents page setup options

type Template

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

Template represents a DOCX document template that can be manipulated by replacing placeholders with HTML or plain text content.

func Open

func Open(path string) (*Template, error)

Open reads a DOCX file from the given path and prepares a Template for manipulation. It loads all document parts into memory for processing.

func (*Template) AddWatermark added in v0.0.5

func (t *Template) AddWatermark(text string) error

AddWatermark adds a text watermark to the document This is a simplified implementation

func (*Template) Apply

func (t *Template) Apply(data map[string]any) error

Apply replaces multiple placeholders in the document using a map. Keys are placeholder names (without braces), and values can be any type that can be stringified (string, int, float, bool, etc.). If a value is a string, it's processed as HTML (SetContent). For other types, it's processed as plain text.

This method also processes template directives like {#if} and {#items} loops before applying regular placeholders.

func (*Template) Cleanup added in v0.0.4

func (t *Template) Cleanup() error

Cleanup removes any remaining placeholders from the document

func (*Template) Close

func (t *Template) Close() error

Close releases the underlying ZIP file reader resources. It should be called when the template is no longer needed.

func (*Template) DetectCharts added in v0.0.5

func (t *Template) DetectCharts() ([]string, error)

DetectCharts finds all charts in the document

func (*Template) GetChartCount added in v0.0.5

func (t *Template) GetChartCount() int

GetChartCount returns the number of charts in the document

func (*Template) HTMLToOOXML

func (t *Template) HTMLToOOXML(htmlContent string) (string, error)

func (*Template) HasCharts added in v0.0.5

func (t *Template) HasCharts() bool

HasCharts checks if the document contains any charts

func (*Template) IncludeFooter added in v0.0.5

func (t *Template) IncludeFooter(footerPath string) error

IncludeFooter includes a footer from an external DOCX file This is a simplified implementation

func (*Template) IncludeHeader added in v0.0.5

func (t *Template) IncludeHeader(headerPath string) error

IncludeHeader includes a header from an external DOCX file This is a simplified implementation

func (*Template) MergeDocuments added in v0.0.5

func (t *Template) MergeDocuments(placeholders map[string]string) error

MergeDocuments merges content from another DOCX file at a placeholder location Syntax: {@include:filename.docx}

func (*Template) ReplaceChartData added in v0.0.5

func (t *Template) ReplaceChartData(chartName string, data ChartData) error

ReplaceChartData replaces data in a chart This is a simplified implementation that demonstrates the concept

func (*Template) ReplaceText

func (t *Template) ReplaceText(placeholder, text string) error

ReplaceText is a simpler function that just replaces text without HTML parsing

func (*Template) Save

func (t *Template) Save(path string) error

Save writes the modified document to the specified output path. It compresses all document parts back into a DOCX (ZIP) file.

func (*Template) SetContent

func (t *Template) SetContent(placeholder, htmlContent string) error

SetContent replaces a placeholder in the document with HTML-formatted content. The placeholder should be provided without curly braces (e.g., "content" for {{content}}). Blocks like tables and lists will be inserted as native Word elements.

func (*Template) SetMetadata added in v0.0.5

func (t *Template) SetMetadata(meta Metadata) error

SetMetadata sets document properties in core.xml

func (*Template) SetPageSetup added in v0.0.5

func (t *Template) SetPageSetup(setup PageSetup) error

SetPageSetup configures page margins and size

func (*Template) SetReadOnly added in v0.0.5

func (t *Template) SetReadOnly(readOnly bool) error

SetReadOnly sets document protection to read-only This is a simplified implementation

type TemplateError added in v0.0.5

type TemplateError struct {
	Op  string // Operation that failed
	Err error  // Original error
}

TemplateError wraps errors with context

func (*TemplateError) Error added in v0.0.5

func (e *TemplateError) Error() string

func (*TemplateError) Unwrap added in v0.0.5

func (e *TemplateError) Unwrap() error

type ValidationError added in v0.0.5

type ValidationError struct {
	Message  string
	Location string // e.g., "placeholder", "loop", "conditional"
	Details  string
}

ValidationError represents a template validation error

func ValidatePlaceholders added in v0.0.5

func ValidatePlaceholders(content string) []ValidationError

ValidatePlaceholders checks if placeholders are properly formatted

func (*ValidationError) Error added in v0.0.5

func (e *ValidationError) Error() string

Jump to

Keyboard shortcuts

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