Documentation
¶
Overview ¶
Package word is a pure-Go port of PHPWord for creating, reading, and filling Microsoft Word documents.
The public API follows PHPWord naming (AddSection, AddText, IOFactory) while using idiomatic Go types and error returns. The library depends only on the Go standard library.
doc := word.New()
section := doc.AddSection()
section.AddText("Hello World")
if err := doc.Save("hello.docx"); err != nil {
log.Fatal(err)
}
Index ¶
- Constants
- func AddHTML(dst htmlContainer, html string, fullHTML ...bool) error
- func AddMarkdown(dst htmlContainer, md string) error
- func DefaultAsianFontName() string
- func DefaultFontColor() string
- func DefaultFontName() string
- func DefaultFontSize() float64
- func DefaultPaper() string
- func ExtractVariables(filename string, readerName ...string) ([]string, error)
- func HasCompatibility() bool
- func IsDefaultRtl() *bool
- func IsOutputEscapingEnabled() bool
- func MeasurementUnit() string
- func SetCompatibility(v bool)
- func SetDefaultAsianFontName(name string)
- func SetDefaultFontColor(color string)
- func SetDefaultFontName(name string)
- func SetDefaultFontSize(size float64)
- func SetDefaultPaper(name string)
- func SetDefaultRtl(v *bool)
- func SetMacroChars(open, close string)
- func SetMacroClosingChars(s string)
- func SetMacroOpeningChars(s string)
- func SetMeasurementUnit(unit string)
- func SetOutputEscapingEnabled(v bool)
- func SetTempDir(dir string)
- func SetZipClass(name string)
- func TempDir() string
- func ZipClass() string
- type BlockData
- type Document
- func (d *Document) AddChart(chartType string, categories []string, seriesData ...[]float64) *element.Chart
- func (d *Document) AddChartStyled(chartType string, categories []string, values []float64, st style.Chart) *element.Chart
- func (d *Document) AddDeletion(text, author, date string, styles ...any) *element.Text
- func (d *Document) AddFontStyle(name string, font any, para ...any)
- func (d *Document) AddHTML(htmlText string) error
- func (d *Document) AddInsertion(text, author, date string, styles ...any) *element.Text
- func (d *Document) AddLinkStyle(name string, font any)
- func (d *Document) AddMarkdown(mdText string) error
- func (d *Document) AddNumberingStyle(name string, num any)
- func (d *Document) AddParagraphStyle(name string, para any)
- func (d *Document) AddSection(st ...any) *element.Section
- func (d *Document) AddTableStyle(name string, table any, firstRow ...any)
- func (d *Document) AddTitleStyle(depth int, font any, para ...any)
- func (d *Document) Bytes() ([]byte, error)
- func (d *Document) CommentOn(text, body, author string, extras ...string) *element.Text
- func (d *Document) Compatibility() *metadata.Compatibility
- func (d *Document) CountStyles() int
- func (d *Document) DocInfo() *metadata.DocInfo
- func (d *Document) EnableTrackChanges(on bool)
- func (d *Document) GetBookmarks() []*element.Bookmark
- func (d *Document) GetCharts() []*element.Chart
- func (d *Document) GetComments() []*element.Comment
- func (d *Document) GetCompatibility() *metadata.Compatibility
- func (d *Document) GetDefaultAsianFontName() string
- func (d *Document) GetDefaultFontColor() string
- func (d *Document) GetDefaultFontName() string
- func (d *Document) GetDefaultFontSize() float64
- func (d *Document) GetDocInfo() *metadata.DocInfo
- func (d *Document) GetEndnotes() []*element.Endnote
- func (d *Document) GetFootnotes() []*element.Footnote
- func (d *Document) GetSection(index int) *element.Section
- func (d *Document) GetSections() []*element.Section
- func (d *Document) GetSettings() *metadata.Settings
- func (d *Document) GetStyle(name string) *NamedStyle
- func (d *Document) GetStyles() []NamedStyle
- func (d *Document) GetTitles() []*element.Title
- func (d *Document) ResetStyles()
- func (d *Document) Save(filename string) error
- func (d *Document) SaveAs(filename, format string) error
- func (d *Document) Sections() []*element.Section
- func (d *Document) SetDefaultAsianFontName(name string)
- func (d *Document) SetDefaultFontColor(color string)
- func (d *Document) SetDefaultFontName(name string)
- func (d *Document) SetDefaultFontSize(size float64)
- func (d *Document) SetDefaultParagraphStyle(para any)
- func (d *Document) Settings() *metadata.Settings
- func (d *Document) SortSections(less func(a, b *element.Section) bool)
- func (d *Document) TrackRevisions() bool
- func (d *Document) WriteTo(dest io.Writer) (int64, error)
- type NamedStyle
- type PhpWord
- type Reader
- type StreamWriter
- type TemplateProcessor
- func (t *TemplateProcessor) ApplyConditionsFromValues(values map[string]string) error
- func (t *TemplateProcessor) Bytes() ([]byte, error)
- func (t *TemplateProcessor) CloneBlock(blockName string, count int) error
- func (t *TemplateProcessor) CloneBlockAndSetValues(blockName string, values []map[string]string) error
- func (t *TemplateProcessor) CloneNestedBlock(blockName string, items []BlockData) error
- func (t *TemplateProcessor) CloneRow(search string, count int) error
- func (t *TemplateProcessor) CloneRowAndSetValues(search string, values []map[string]string) error
- func (t *TemplateProcessor) DeleteBlock(blockName string) error
- func (t *TemplateProcessor) DeleteRow(search string) error
- func (t *TemplateProcessor) GetVariableCount() map[string]int
- func (t *TemplateProcessor) GetVariables() []string
- func (t *TemplateProcessor) ReplaceBlock(blockName, replacement string) error
- func (t *TemplateProcessor) ReplaceCarriageReturns(s string) string
- func (t *TemplateProcessor) ReplaceXmlBlock(macroName, block, blockType string) error
- func (t *TemplateProcessor) Save(filename string) error
- func (t *TemplateProcessor) SaveAs(filename string) error
- func (t *TemplateProcessor) SetChart(search string, ch *element.Chart) error
- func (t *TemplateProcessor) SetCheckbox(search string, checked bool)
- func (t *TemplateProcessor) SetComplexBlock(search string, el element.Element) error
- func (t *TemplateProcessor) SetComplexValue(search string, el element.Element) error
- func (t *TemplateProcessor) SetCondition(name string, keep bool) error
- func (t *TemplateProcessor) SetConditions(conds map[string]bool) error
- func (t *TemplateProcessor) SetImageValue(search, path string) error
- func (t *TemplateProcessor) SetImageValueBytes(search, filename string, data []byte) error
- func (t *TemplateProcessor) SetMacroChars(open, close string)
- func (t *TemplateProcessor) SetMacroClosingChars(s string)
- func (t *TemplateProcessor) SetMacroOpeningChars(s string)
- func (t *TemplateProcessor) SetUpdateFields(update bool)
- func (t *TemplateProcessor) SetValue(search, replace string)
- func (t *TemplateProcessor) SetValueLimit(search, replace string, limit int)
- func (t *TemplateProcessor) SetValues(values map[string]string)
- type Writer
Constants ¶
const ( ChartTypeBar = "bar" ChartTypeColumn = "column" ChartTypeLine = "line" ChartTypePie = "pie" )
Chart type names used by AddChart and Container.AddChart.
const ( UnitTwip = "twip" UnitCM = "cm" UnitMM = "mm" UnitInch = "inch" UnitPoint = "point" UnitPica = "pica" )
Variables ¶
This section is empty.
Functions ¶
func AddHTML ¶
AddHTML parses HTML and appends equivalent elements to dst (PHPWord Shared\Html::addHtml).
func AddMarkdown ¶ added in v0.4.0
AddMarkdown parses Markdown and appends native GoWord elements to dst.
func DefaultAsianFontName ¶
func DefaultAsianFontName() string
DefaultAsianFontName returns the process-wide default East-Asian font.
func DefaultFontColor ¶
func DefaultFontColor() string
DefaultFontColor returns the process-wide default color (hex, no #).
func DefaultFontName ¶
func DefaultFontName() string
DefaultFontName returns the process-wide default font.
func DefaultFontSize ¶
func DefaultFontSize() float64
DefaultFontSize returns the process-wide default size in points.
func DefaultPaper ¶
func DefaultPaper() string
DefaultPaper returns the process-wide default paper size name.
func ExtractVariables ¶
ExtractVariables returns ${placeholder} names from a template file.
func HasCompatibility ¶
func HasCompatibility() bool
HasCompatibility reports XMLWriter compatibility mode (PHP Settings::hasCompatibility).
func IsDefaultRtl ¶
func IsDefaultRtl() *bool
IsDefaultRtl reports the process-wide default RTL flag.
func IsOutputEscapingEnabled ¶
func IsOutputEscapingEnabled() bool
IsOutputEscapingEnabled reports whether XML escaping is on.
func MeasurementUnit ¶
func MeasurementUnit() string
MeasurementUnit returns the process-wide unit (twip, cm, mm, inch, point, pica).
func SetCompatibility ¶
func SetCompatibility(v bool)
SetCompatibility toggles XMLWriter compatibility mode.
func SetDefaultAsianFontName ¶
func SetDefaultAsianFontName(name string)
SetDefaultAsianFontName sets the process-wide default East-Asian font.
func SetDefaultFontColor ¶
func SetDefaultFontColor(color string)
SetDefaultFontColor sets the process-wide default color.
func SetDefaultFontName ¶
func SetDefaultFontName(name string)
SetDefaultFontName sets the process-wide default font.
func SetDefaultFontSize ¶
func SetDefaultFontSize(size float64)
SetDefaultFontSize sets the process-wide default size in points.
func SetDefaultPaper ¶
func SetDefaultPaper(name string)
SetDefaultPaper sets the process-wide default paper size name.
func SetDefaultRtl ¶
func SetDefaultRtl(v *bool)
SetDefaultRtl sets the process-wide default RTL flag.
func SetMacroChars ¶
func SetMacroChars(open, close string)
SetMacroChars sets both placeholder delimiters.
func SetMacroClosingChars ¶
func SetMacroClosingChars(s string)
SetMacroClosingChars sets the placeholder closing delimiter.
func SetMacroOpeningChars ¶
func SetMacroOpeningChars(s string)
SetMacroOpeningChars sets the placeholder opening delimiter (PHPWord).
func SetMeasurementUnit ¶
func SetMeasurementUnit(unit string)
SetMeasurementUnit sets the process-wide unit.
func SetOutputEscapingEnabled ¶
func SetOutputEscapingEnabled(v bool)
SetOutputEscapingEnabled toggles XML escaping of text (always recommended).
func SetZipClass ¶
func SetZipClass(name string)
SetZipClass is accepted for PHP parity; Go always uses archive/zip.
Types ¶
type BlockData ¶ added in v0.3.0
BlockData is one clone of a named template block, including nested blocks and ${if} conditions scoped to that instance.
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document is the PHPWord PhpWord class: an in-memory word-processing document.
func Load ¶
Load loads a document (PHPWord IOFactory::load). If readerName is omitted, the format is inferred from the file extension.
func (*Document) AddChart ¶ added in v0.4.0
func (d *Document) AddChart(chartType string, categories []string, seriesData ...[]float64) *element.Chart
AddChart appends a native OpenXML chart to the last (or a new) section. seriesData is one or more value series aligned with categories.
func (*Document) AddChartStyled ¶ added in v0.4.0
func (d *Document) AddChartStyled(chartType string, categories []string, values []float64, st style.Chart) *element.Chart
AddChartStyled is AddChart with DrawingML chart style options.
func (*Document) AddDeletion ¶ added in v0.4.0
AddDeletion appends tracked-delete text (w:del).
func (*Document) AddFontStyle ¶
AddFontStyle registers a named character style (PHPWord addFontStyle).
func (*Document) AddInsertion ¶ added in v0.4.0
AddInsertion appends tracked-insert text (w:ins).
func (*Document) AddLinkStyle ¶
AddLinkStyle registers the Hyperlink character style.
func (*Document) AddMarkdown ¶ added in v0.4.0
AddMarkdown parses Markdown into the last (or a new) section.
func (*Document) AddNumberingStyle ¶
AddNumberingStyle registers a named numbering definition.
func (*Document) AddParagraphStyle ¶
AddParagraphStyle registers a named paragraph style.
func (*Document) AddSection ¶
AddSection appends a section (PHPWord addSection).
func (*Document) AddTableStyle ¶
AddTableStyle registers a named table style.
func (*Document) AddTitleStyle ¶
AddTitleStyle registers HeadingN (depth 1-9). depth 0 is Title.
func (*Document) CommentOn ¶ added in v0.4.0
CommentOn annotates text in the last (or a new) section. extras is optional initials then date.
func (*Document) Compatibility ¶
func (d *Document) Compatibility() *metadata.Compatibility
Compatibility returns OOXML compatibility settings.
func (*Document) CountStyles ¶
CountStyles returns the number of registered named styles.
func (*Document) EnableTrackChanges ¶ added in v0.4.0
EnableTrackChanges turns on document-level revision tracking (w:trackRevisions).
func (*Document) GetBookmarks ¶
GetBookmarks returns bookmark elements.
func (*Document) GetComments ¶
GetComments returns comment elements, including those attached as ranges.
func (*Document) GetCompatibility ¶
func (d *Document) GetCompatibility() *metadata.Compatibility
GetCompatibility is the PHPWord name for Compatibility.
func (*Document) GetDefaultAsianFontName ¶
func (*Document) GetDefaultFontColor ¶
func (*Document) GetDefaultFontName ¶
func (*Document) GetDefaultFontSize ¶
func (*Document) GetDocInfo ¶
GetDocInfo is the PHPWord name for DocInfo.
func (*Document) GetEndnotes ¶
GetEndnotes returns endnote elements.
func (*Document) GetFootnotes ¶
GetFootnotes returns footnote elements.
func (*Document) GetSection ¶
GetSection returns the section at index, or nil.
func (*Document) GetSections ¶
GetSections is the PHPWord name for Sections.
func (*Document) GetSettings ¶
GetSettings is the PHPWord name for Settings.
func (*Document) GetStyle ¶
func (d *Document) GetStyle(name string) *NamedStyle
GetStyle returns a named style, or nil.
func (*Document) GetStyles ¶
func (d *Document) GetStyles() []NamedStyle
GetStyles returns registered named styles (PHPWord Style::getStyles).
func (*Document) ResetStyles ¶
func (d *Document) ResetStyles()
ResetStyles clears the named-style registry (PHPWord Style::resetStyles).
func (*Document) SetDefaultAsianFontName ¶
func (*Document) SetDefaultFontColor ¶
func (*Document) SetDefaultFontName ¶
func (*Document) SetDefaultFontSize ¶
func (*Document) SetDefaultParagraphStyle ¶
SetDefaultParagraphStyle sets the Normal style.
func (*Document) SortSections ¶
SortSections sorts sections with the given comparison.
func (*Document) TrackRevisions ¶ added in v0.4.0
TrackRevisions reports whether revision tracking is on.
type NamedStyle ¶
type NamedStyle struct {
Name string
Kind string
Font *style.Font
Paragraph *style.Paragraph
Table *style.Table
FirstRow *style.Table
Numbering *style.Numbering
Depth int
}
NamedStyle is a document-scoped style definition (PHPWord Style registry entry).
type PhpWord ¶
type PhpWord = Document
PhpWord is an alias for Document, matching the PHP class name.
type Reader ¶
Reader loads a document from a file.
func CreateReader ¶
CreateReader returns a reader for the named format (PHPWord IOFactory::createReader).
type StreamWriter ¶ added in v0.2.0
type StreamWriter struct {
// contains filtered or unexported fields
}
StreamWriter writes paragraphs and tables incrementally into a Word2007 (.docx) ZIP stream. document.xml is opened on the first write so large tables never accumulate as a single in-memory XML tree.
Close finishes document.xml and emits the remaining OOXML parts (content types, relationships, styles, media). StreamWriter is not safe for concurrent use.
func NewStreamWriter ¶ added in v0.2.0
func NewStreamWriter(dest io.Writer) *StreamWriter
NewStreamWriter starts a Word2007 package on dest.
func (*StreamWriter) BytesWritten ¶ added in v0.2.0
func (s *StreamWriter) BytesWritten() int64
BytesWritten returns the number of bytes written to dest so far.
func (*StreamWriter) Close ¶ added in v0.2.0
func (s *StreamWriter) Close() error
Close finishes document.xml and writes the remaining package parts.
func (*StreamWriter) WriteElement ¶ added in v0.2.0
func (s *StreamWriter) WriteElement(el element.Element) error
WriteElement writes any document body element into the stream.
func (*StreamWriter) WriteParagraph ¶ added in v0.2.0
func (s *StreamWriter) WriteParagraph(text string, styles ...any) error
WriteParagraph writes a paragraph of text into the open document.xml stream. styles follows Section.AddText: optional font then paragraph style.
func (*StreamWriter) WriteTable ¶ added in v0.2.0
func (s *StreamWriter) WriteTable(tbl *element.Table) error
WriteTable writes a table, flushing XML after each row.
type TemplateProcessor ¶
type TemplateProcessor struct {
// contains filtered or unexported fields
}
TemplateProcessor fills ${placeholders} in an existing .docx (PHPWord TemplateProcessor).
func NewTemplateProcessor ¶
func NewTemplateProcessor(filename string) (*TemplateProcessor, error)
NewTemplateProcessor opens a .docx template from disk.
func NewTemplateProcessorBytes ¶
func NewTemplateProcessorBytes(data []byte) (*TemplateProcessor, error)
NewTemplateProcessorBytes opens a .docx template from memory.
func (*TemplateProcessor) ApplyConditionsFromValues ¶ added in v0.3.0
func (t *TemplateProcessor) ApplyConditionsFromValues(values map[string]string) error
ApplyConditionsFromValues treats empty/"0"/"false"/"no"/"off" as false.
func (*TemplateProcessor) Bytes ¶
func (t *TemplateProcessor) Bytes() ([]byte, error)
Bytes returns the filled template as a .docx package.
func (*TemplateProcessor) CloneBlock ¶
func (t *TemplateProcessor) CloneBlock(blockName string, count int) error
CloneBlock clones the region between ${block} and ${/block} count times. Nested ${inner}...${/inner} markers are indexed (${inner#1}) so they can be cloned per instance. Matching uses nesting depth for same-named blocks.
func (*TemplateProcessor) CloneBlockAndSetValues ¶
func (t *TemplateProcessor) CloneBlockAndSetValues(blockName string, values []map[string]string) error
CloneBlockAndSetValues clones ${block}...${/block} once per values row (PHPWord).
func (*TemplateProcessor) CloneNestedBlock ¶ added in v0.3.0
func (t *TemplateProcessor) CloneNestedBlock(blockName string, items []BlockData) error
CloneNestedBlock clones ${name}...${/name} once per item, then clones nested blocks and applies values / conditions with #n suffixes.
func (*TemplateProcessor) CloneRow ¶
func (t *TemplateProcessor) CloneRow(search string, count int) error
CloneRow clones the table row that contains ${search} count times, renaming macros to ${search#1}, ${search#2}, ... Vertically merged (w:vMerge restart/continue) rows are cloned as a single group.
func (*TemplateProcessor) CloneRowAndSetValues ¶
func (t *TemplateProcessor) CloneRowAndSetValues(search string, values []map[string]string) error
CloneRowAndSetValues clones a row and fills ${name#n} from values.
func (*TemplateProcessor) DeleteBlock ¶
func (t *TemplateProcessor) DeleteBlock(blockName string) error
DeleteBlock removes the region between ${block} and ${/block}.
func (*TemplateProcessor) DeleteRow ¶
func (t *TemplateProcessor) DeleteRow(search string) error
DeleteRow removes the table row that contains ${search}.
func (*TemplateProcessor) GetVariableCount ¶
func (t *TemplateProcessor) GetVariableCount() map[string]int
GetVariableCount returns placeholder name → occurrence count (PHPWord getVariableCount).
func (*TemplateProcessor) GetVariables ¶
func (t *TemplateProcessor) GetVariables() []string
GetVariables returns unique placeholder names in first-seen order.
func (*TemplateProcessor) ReplaceBlock ¶
func (t *TemplateProcessor) ReplaceBlock(blockName, replacement string) error
ReplaceBlock replaces the region between ${block} and ${/block}.
func (*TemplateProcessor) ReplaceCarriageReturns ¶
func (t *TemplateProcessor) ReplaceCarriageReturns(s string) string
ReplaceCarriageReturns turns newlines into Word break runs (PHPWord).
func (*TemplateProcessor) ReplaceXmlBlock ¶
func (t *TemplateProcessor) ReplaceXmlBlock(macroName, block, blockType string) error
ReplaceXmlBlock replaces the XML element of blockType that contains ${macro}.
func (*TemplateProcessor) Save ¶
func (t *TemplateProcessor) Save(filename string) error
Save writes the filled template to filename.
func (*TemplateProcessor) SaveAs ¶
func (t *TemplateProcessor) SaveAs(filename string) error
SaveAs is an alias for Save (PHPWord).
func (*TemplateProcessor) SetChart ¶
func (t *TemplateProcessor) SetChart(search string, ch *element.Chart) error
SetChart replaces ${search} with a chart drawing and chart XML part (PHPWord).
func (*TemplateProcessor) SetCheckbox ¶
func (t *TemplateProcessor) SetCheckbox(search string, checked bool)
SetCheckbox toggles a content-control checkbox at ${search}.
func (*TemplateProcessor) SetComplexBlock ¶
func (t *TemplateProcessor) SetComplexBlock(search string, el element.Element) error
SetComplexBlock replaces the w:p containing ${search} with the rendered element.
func (*TemplateProcessor) SetComplexValue ¶
func (t *TemplateProcessor) SetComplexValue(search string, el element.Element) error
SetComplexValue replaces the w:r containing ${search} with the rendered element.
func (*TemplateProcessor) SetCondition ¶ added in v0.3.0
func (t *TemplateProcessor) SetCondition(name string, keep bool) error
SetCondition keeps or clips ${if name}...${endif}.
func (*TemplateProcessor) SetConditions ¶ added in v0.3.0
func (t *TemplateProcessor) SetConditions(conds map[string]bool) error
SetConditions applies many named ${if} blocks.
func (*TemplateProcessor) SetImageValue ¶
func (t *TemplateProcessor) SetImageValue(search, path string) error
SetImageValue replaces a placeholder with a drawing that references a new image part.
func (*TemplateProcessor) SetImageValueBytes ¶
func (t *TemplateProcessor) SetImageValueBytes(search, filename string, data []byte) error
SetImageValueBytes embeds image bytes at ${search}.
func (*TemplateProcessor) SetMacroChars ¶
func (t *TemplateProcessor) SetMacroChars(open, close string)
func (*TemplateProcessor) SetMacroClosingChars ¶
func (t *TemplateProcessor) SetMacroClosingChars(s string)
func (*TemplateProcessor) SetMacroOpeningChars ¶
func (t *TemplateProcessor) SetMacroOpeningChars(s string)
func (*TemplateProcessor) SetUpdateFields ¶
func (t *TemplateProcessor) SetUpdateFields(update bool)
SetUpdateFields sets w:updateFields in word/settings.xml.
func (*TemplateProcessor) SetValue ¶
func (t *TemplateProcessor) SetValue(search, replace string)
SetValue replaces ${search} with replace across document parts.
func (*TemplateProcessor) SetValueLimit ¶
func (t *TemplateProcessor) SetValueLimit(search, replace string, limit int)
SetValueLimit replaces at most limit occurrences (-1 = all).
func (*TemplateProcessor) SetValues ¶
func (t *TemplateProcessor) SetValues(values map[string]string)
SetValues replaces many placeholders.