Skip to content

OpenXML Compatibility ​

GoWord writes Office Open XML WordprocessingML 2007 packages (ECMA-376 / ISO/IEC 29500 transitional). Microsoft Word 2007 through Microsoft 365 open the files. WPS Office and LibreOffice open the common subset (paragraphs, tables, images); DrawingML charts and wps:wsp shapes are Word-oriented.

Package layout ​

PartRequiredNotes
[Content_Types].xmlYesOverrides for document.xml, headers, charts, media
_rels/.relsYesPoints at word/document.xml
word/document.xmlYesBody + last w:sectPr
word/_rels/document.xml.relsYesImages, headers, footers, charts, numbering
word/styles.xmlYesNamed styles plus default Normal
word/media/imageN.*If imagesNames assigned at write time
word/charts/chartN.xmlIf chartsOne part per AddChart
word/headerN.xml / footerN.xmlIf headersReferenced from w:sectPr via r:id

Schemas the writer targets ​

FeatureNamespace / nodeWord behaviour
Body textw: WordprocessingMLParagraphs, tables, sectPr
Native equationsm: Office Math (m:oMathPara, m:oMath)Double-click opens the equation editor
Chartsc: ECMA-376 ChartSeries order is idx → order → tx → spPr
Word 2010 shapeswps:wsp / a:prstGeomRect, roundRect, rightArrow, text box
Fieldsw:instrText (PAGE, TOC, NUMPAGES)Refresh with Update Field
Protectionw:documentProtectionRead-only / comments / tracked changes / forms
Edit exceptionsw:permStart / w:permEndAllowEdit ranges stay writable
WatermarkVML PowerPlusWaterMarkObject in headersDiagonal or tiled word-art; WordPictureWatermark for images
SDTw:sdt / w:sdtPr / w:sdtContentPlain text, drop-down, date, w14:checkbox
Table row / cellw:tblHeader, w:cantSplit, w:vAlign, w:textDirectionRepeating headers, unbreakable rows, vertical text

Strict rules the test suite enforces ​

The package openxml_strict_test.go rejects output that Word would offer to repair:

  • Every w:tc contains at least one w:p.
  • Template replacements never emit raw & inside w:t (no P style numeric entities for ordinary ASCII).
  • Combo charts use two distinct c:axId values; the secondary series points at the second value axis.
  • Area charts omit c:dLblPos (the area XSD does not allow it).
  • c:legend sits after c:plotArea, never inside it.
  • Line markers emit c:symbol plus c:size 5.
  • w:cols uses w:sep, not w:separator.

Complete example — round-trip through the reader ​

go
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/yunkeweb/go-word"
)

func main() {
	doc := word.New()
	sec := doc.AddSection()
	sec.AddTitle("Compatibility", 1)
	sec.AddText("Round-trip through CreateReader.")
	sec.AddMath(`x^{2}`)
	if err := doc.Save("compat.docx"); err != nil {
		log.Fatal(err)
	}

	r, err := word.CreateReader("Word2007")
	if err != nil {
		log.Fatal(err)
	}
	loaded, err := r.Load("compat.docx")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(loaded.ExtractText())

	_ = os.Remove("compat.docx")
}

ExtractText walks the reconstructed DOM. For files too large to load, use StreamExtractText. There is no word.ReadDOM; the DOM loaders are word.Open, word.Read, and word.Load.

Full-feature matrix (v0.9.0) ​

tests/matrix writes 80 randomly combined documents covering every exported module from v0.1.0 through v0.9.0: typography, multi-section layout, headers/footers, tables, images/shapes, TOC/bookmarks/comments, OMML, charts, SDT, and watermark/protection. Output stays in ./test_output_docs (gitignored).

sh
go run ./tests/matrix
go run tests/matrix/validate_reader.go
EngineWhat it checksv0.9.0 result
word.Open / word.Read + ExtractText / ExtractImagesDOM reverse-parse, no panic, error == nil80 PASS
word.StreamExtractText / word.StreamExtractImagesStreaming extract, no panic, error == nil80 PASS
Microsoft Word COM (DisplayAlerts=0, OpenNoRepairDialog)OpenXML repair dialogs, node order, parse exceptions80 PASS

When Word shows “the file is corrupt”, start at FAQ. Chart XSD order is documented on Charts. Recipe 4 on Enterprise Recipes combines SDT, repeating headers, tiled watermarks, and AllowEdit.

DOCX to HTML ​

The reader reconstructs a DOCX DOM that can be rendered as an HTML fragment or a standalone page:

go
html, err := doc.RenderHTML(word.HTMLOptions{
	Standalone: true,
	IncludeCSS: true,
})

Use word.RenderHTMLFile or word.RenderHTMLWithOptions when the input is still a file or reader. These functions return bytes for the caller to save. Images default to embedded data URIs; use ImageURL to persist assets and return their public URLs, or HTMLImageURL to reuse existing URLs. Neither mode automatically hosts media files. RenderHTMLWithDiagnostics reports unsupported elements retained in the DOM; Strict: true turns those diagnostics into an error. Content omitted during reading cannot be diagnosed by the renderer, so strict success does not imply lossless conversion. See the DOCX to HTML guide for complete programs, supported content, and read limits.

Released under the GNU LGPL v3.0.