docx

package
v0.0.0-...-7fc396a Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DOCX_NEWLINE_INJECT        = `</w:t><w:br/><w:t>`
	DOCX_BREAKPARAGRAPH_INJECT = `</w:t></w:r></w:p><w:p><w:r><w:t>`

	STYLE_WRAPPER_F     = `<w:rPr>%s</w:rPr><w:t>%s</w:t>`
	BOLD_W_TAG          = `<w:b /><w:bCs />`
	ITALIC_W_TAG        = `<w:i /><w:iCs />`
	UNDERLINE_W_TAG     = `<w:u w:val="single"/>`
	STRIKETHROUGH_W_TAG = `<w:strike />`
	FONT_SIZE_W_TAGS_F  = `<w:sz w:val="%d" /><w:szCs w:val="%d" />`
	COLOR_W_TAG_F       = `<w:color w:val="%s" />`
	HIGHLIGHT_W_TAG_F   = `<w:highlight w:val="%s" />`
	// HIGHLIGHT all values: https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.highlightcolor?view=openxml-2.8.1
	SHADING_W_TAG_F = `<w:shd w:val="clear" w:color="auto" w:fill="%s"/>`
)
View Source
const (
	FONT_SIZE_STYLE_PREFIX       = "fontSize:"
	FONT_SIZE_STYLE_PREFIX_SHORT = "fs:"
	TEXT_SHADING_STYLE_PREFIX    = "bg:"
)
View Source
const DOC_PR_ID_ROOF = 2_147_483_647 // docx id attributes are 32-bit signed integers
View Source
const (
	ImageMediaType = iota + 1
)

Variables

View Source
var (
	BOLD_WRAPPER_F          = fmt.Sprintf(STYLE_WRAPPER_F, BOLD_W_TAG, "%s")
	ITALIC_WRAPPER_F        = fmt.Sprintf(STYLE_WRAPPER_F, ITALIC_W_TAG, "%s")
	UNDERLINE_WRAPPER_F     = fmt.Sprintf(STYLE_WRAPPER_F, UNDERLINE_W_TAG, "%s")
	STRIKETHROUGH_WRAPPER_F = fmt.Sprintf(STYLE_WRAPPER_F, STRIKETHROUGH_W_TAG, "%s")
	COLOR_WRAPPER_F         = fmt.Sprintf(STYLE_WRAPPER_F, COLOR_W_TAG_F, "%s")
	HIGHLIGHT_WRAPPER_F     = fmt.Sprintf(STYLE_WRAPPER_F, HIGHLIGHT_W_TAG_F, "%s")
	SHADING_WRAPPER_F       = fmt.Sprintf(STYLE_WRAPPER_F, SHADING_W_TAG_F, "%s")
)
View Source
var TemplateFuncs = template.FuncMap{
	"list":             list,
	"bold":             bold,
	"italic":           italic,
	"underline":        underline,
	"strike":           strike,
	"fontSize":         fontSize,
	"inlineStyledText": inlineStyledText,
	"styledText":       styledText,
	"color":            color,
	"highlight":        highlight,
	"preserveNewline":  preserveNewline,
	"breakParagraph":   breakParagraph,
	"shadeTextBg":      shadeTextBg,
	"image":            image,
	"replaceImage":     replaceImage,
	"shapeBgFillColor": shapeBgFillColor,
	"tableCellBgColor": tableCellBgColor,
}

Functions

func ApplyTemplateToXml

func ApplyTemplateToXml(f *zip.File, templateValues any, templateFuncs template.FuncMap) ([]byte, error)

func ExtractChartFilename

func ExtractChartFilename(path string) (string, error)

ExtractChartFilename now only works with a single submatch

func ParseContentTypes

func ParseContentTypes(data []byte) (*contentTypes, error)

func ParseDocumentMeta

func ParseDocumentMeta(zm goziputils.ZipMap, tf template.FuncMap) (*documentMeta, error)

TODO: use xml parsing instead of regex

func PatchXml

func PatchXml(srcXml string) string

PatchXml removes automatically insert content between template expressions (EG: "{{ .Text }}" could have correctors highlights tags separating the expressions tokens).

func UpdateChart

func UpdateChart(fileContent []byte, cellAndValues map[string]string) ([]byte, error)

TODO: parse and unmarshal xml instead of using regex

Types

type Media

type Media struct {
	Data         []byte
	WordFilename string
}

type MediaMap

type MediaMap map[string]*Media

type MediaRel

type MediaRel struct {
	Type   uint
	RefID  string
	Source string
}

type Relationship

type Relationship struct {
	XMLName       xml.Name             `xml:"http://schemas.openxmlformats.org/package/2006/relationships Relationships"`
	Relationships []relationshipDetail `xml:"Relationship"`
}

func ParseRelationship

func ParseRelationship(data []byte) (*Relationship, error)

func (*Relationship) AddMediaToRels

func (r *Relationship) AddMediaToRels(media []MediaRel)

func (*Relationship) ToXml

func (r *Relationship) ToXml() ([]byte, error)

type XmlImageData

type XmlImageData struct {
	DocPrId uint32
	Name    string
	RefID   string
	Cx      int
	Cy      int
}

Jump to

Keyboard shortcuts

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