Documentation
¶
Overview ¶
Package ooxml reads Office Open XML documents into the neutral github.com/go-richdoc/richdoc model — Word (.docx), Excel (.xlsx) and PowerPoint (.pptx) — and writes them: WriteWord and WriteWorkbook from a document, WritePresentation from a deck of rendered slides.
One container, three vocabularies ¶
⛔ This is the opposite of ODF, and the difference decides the shape of the package. In OpenDocument, office:text, office:spreadsheet, office:presentation and office:drawing are four BODIES that share one vocabulary: text:p is a paragraph in all of them, so one walker reads all four. In OOXML the three formats share the CONTAINER — the ZIP, the content types, the relationships, all of which are github.com/go-ooxml/opc — and then speak three different languages:
Word w:p > w:r > w:t (WordprocessingML)
PowerPoint a:p > a:r > a:t (DrawingML, inside p:sp)
Excel c > v, where v may be an INDEX (SpreadsheetML)
into xl/sharedStrings.xml
So there is one reader per format here, and the thing they share lives one repository down.
Index ¶
- Constants
- func ParsePresentation(src []byte, lim opc.Limits) (*richdoc.Document, error)
- func ParseWord(src []byte, lim opc.Limits) (*richdoc.Document, error)
- func ParseWorkbook(src []byte, lim opc.Limits) (*richdoc.Document, error)
- func WritePresentation(p Presentation) ([]byte, error)
- func WriteWord(d *richdoc.Document) ([]byte, error)
- func WriteWorkbook(d *richdoc.Document) ([]byte, error)
- type Fill
- type FillKind
- type Font
- type Geometry
- type GradientStop
- type Image
- type PathCmd
- type Point
- type Presentation
- type PresentationSlide
- type RGBA
- type Shape
- type TextRun
Constants ¶
const ( RelHyperlink = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" RelImage = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" )
RelHyperlink and RelImage are the relationship types a paragraph's contents point through.
Variables ¶
This section is empty.
Functions ¶
func ParsePresentation ¶ added in v0.2.0
ParsePresentation reads a PresentationML package (.pptx) into a document: each slide as a heading of its position, followed by the text found in its shapes.
⛔ The slides come out in the order the PRESENTATION gives, which is p:sldIdLst — not the order of the relationships, and above all not the order of the filenames. slide1.xml, slide2.xml … happen to be in order in a file straight out of a converter, and stop being in order the moment somebody moves a slide: the names stay where they were and the list changes. A reader that sorted by filename would silently reorder the deck.
func ParseWorkbook ¶ added in v0.2.0
ParseWorkbook reads a SpreadsheetML package (.xlsx) into a document: each sheet as a heading of its own name, followed by its cells as a table.
⛔ A cell's value is OFTEN NOT IN THE CELL. With t="s" — which is what every piece of text in a real workbook uses — the <v> holds an INDEX into xl/sharedStrings.xml, so a reader that took <v> as the text produces a table of "0", "1", "2" … in place of every word. Measured on a file LibreOffice wrote from a three-line CSV: every one of its five text cells was an index.
func WritePresentation ¶ added in v0.4.0
func WritePresentation(p Presentation) ([]byte, error)
WritePresentation writes a deck as a PresentationML package (.pptx).
A slide is a picture (what marp-cli's --pptx writes), editable shapes (Shapes: text boxes, filled and gradient shapes, pictures — set in fonts the deck can embed, so that it is the same slide in every viewer), or both. ParsePresentation reads back the text boxes' words; a picture's are in the picture.
⛔ The output is deterministic: the same Presentation gives the same bytes. No clock is read (see Presentation.Created) and every id is a position.
func WriteWord ¶ added in v0.3.0
WriteWord writes a document as a WordprocessingML package (.docx).
⛔ What comes back is a package, not a rendering. Word decides how wide a column is, where a page breaks and what a heading looks like; this says which paragraph is a heading and which cells are a row, and the styles part below gives those names something to mean.
⛔ It is NOT a round trip with ParseWord. A document read from Word and written back out loses everything richdoc has no word for — the fonts, the margins, the section properties, the revision history — because richdoc is a document model and not a .docx model. Said here because "write what you read" is the obvious thing to expect and it is not what this does.
func WriteWorkbook ¶ added in v0.3.0
WriteWorkbook writes a document's tables as a SpreadsheetML package (.xlsx), one sheet per table.
⛔ Only the tables. A spreadsheet is a grid of cells and a document is not, so the headings and paragraphs around a table have nowhere to go — and putting them in column A of a sheet of their own would make a spreadsheet that looks like a document and works as neither. A document with no tables in it gives a workbook with one empty sheet rather than an error, because "there was nothing to put in a grid" is an answer.
Types ¶
type Fill ¶ added in v0.5.0
type Fill struct {
Kind FillKind
Color RGBA
Stops []GradientStop
Angle float64
Radial bool
Center [2]float64
}
Fill is how a shape's inside, or a slide's background, is painted (§20.1.8). A gradient's Stops are at positions from 0 to 1, increasing; a linear one runs at Angle degrees clockwise from the x axis across the shape's box (a:lin, unscaled), a radial one (a:path path="circle") from its centre, at Center as fractions of the box, outwards to its corners.
type Font ¶ added in v0.5.0
Font is a typeface embedded in the deck (p:embeddedFontLst, §19.2.1.10): TrueType or OpenType data for its regular, bold, italic and bold italic faces, any of which may be nil. Typeface is the name text runs give.
type Geometry ¶ added in v0.5.0
Geometry is a shape's outline (§20.1.9). Preset is one of the preset shapes: "" or "rect", "roundRect" (corner radius Radius EMU), "ellipse". Paths, when not empty, is a custom geometry instead (a:custGeom): each path a closed outline of commands in EMU, relative to the shape's top-left corner. Several paths make one outline whose overlaps are holes, which is how a ring is drawn.
type GradientStop ¶ added in v0.5.0
GradientStop is one colour of a gradient.
type Image ¶ added in v0.5.0
Image is a picture shapes refer to by its position in Presentation.Images, plus one: each is stored once whatever number of slides show it.
type PathCmd ¶ added in v0.5.0
PathCmd is one command of a custom path: 'M' move to P[0], 'L' line to P[0], 'C' cubic Bézier through P[0] and P[1] to P[2], 'Z' close.
type Presentation ¶ added in v0.4.0
type Presentation struct {
Width, Height int64
Slides []PresentationSlide
// The document properties, written to docProps/core.xml when set: dc:title,
// dc:creator, dc:subject and cp:keywords.
Title, Author, Subject, Keywords string
// Created is written as both dcterms:created and dcterms:modified. The
// zero time writes neither, which is what makes the output the same
// bytes for the same input: a writer that stamped the clock could not be
// diffed, cached or reproduced.
Created time.Time
// Images are the pictures editable slides' shapes show (Shape.Image),
// each stored once in the package however many slides show it.
Images []Image
// Fonts are typefaces embedded in the deck, for viewers that do not
// have them (p:embeddedFontLst, §19.2.1.10). Embed only fonts whose
// licence allows it.
Fonts []Font
}
A Presentation is a deck to write as a .pptx: one full-slide picture per slide, with its speaker notes.
Width and Height are the slide size in EMU (English Metric Units, 914400 to the inch). A CSS pixel is 9525 EMU, so a 1280×720 deck is 12192000×6858000, which is PowerPoint's own 16:9.
type PresentationSlide ¶ added in v0.4.0
type PresentationSlide struct {
// Image is the picture, stretched over the whole slide; render it at the
// slide's aspect ratio. ImageType is "png" or "jpeg", and is checked
// against the image's own signature. It may be left out when the slide
// has Shapes or a Background.
Image []byte
ImageType string
// Background is the slide's background (p:bg); nil leaves the master's,
// which is none.
Background *Fill
// Shapes are the slide's editable content, back to front.
Shapes []Shape
// Notes are the speaker notes, one paragraph per line. Empty writes no
// notes slide, as ECMA-376 §13.3.5 asks: one per slide THAT HAS notes.
Notes string
// Name names the slide (p:cSld name) and is the picture's alternative
// text, which is what a screen reader says for a slide that is a picture.
// Empty gives "Slide N".
Name string
}
A PresentationSlide is one slide: a picture that covers it, or editable shapes, or both (the picture at the back), and its notes.
type RGBA ¶ added in v0.5.0
type RGBA struct{ R, G, B, A uint8 }
RGBA is a colour with an alpha: 255 is opaque.
type Shape ¶ added in v0.5.0
type Shape struct {
X, Y, W, H int64
// Geometry is the outline; the zero value is a rectangle.
Geometry Geometry
// Fill paints the inside of the geometry. The zero value is no fill.
Fill Fill
// Text makes the shape a text box: one paragraph of runs, set from the
// box's top-left corner with no inset and no wrapping, so that it stays
// on the line it was laid out on. An empty Text is a plain shape.
Text []TextRun
// Image, when not zero, makes the shape a picture of Presentation.Images
// [Image-1], stretched over the box. Geometry, Fill and Text are then
// ignored. Crop removes the given fractions of the picture from its
// left, top, right and bottom before it is stretched (a:srcRect).
Image int
Crop [4]float64
// Name names the shape (p:cNvPr name); for a picture it is also the
// alternative text. Empty gives a numbered name.
Name string
}
A Shape is one element of an editable slide: a DrawingML shape (p:sp, ECMA-376 Part 1 §19.3.1.43) — a geometry with a fill, and optionally text — or a picture (p:pic, §19.3.1.37). Shapes are drawn in order, the first at the back.
Positions and sizes are in EMU, 914400 to the inch (a CSS pixel is 9525).
type TextRun ¶ added in v0.5.0
type TextRun struct {
Text string
Font string
Size float64
Bold, Italic bool
Color RGBA
Link string
// Spacing is added after each character, in points (a:rPr spc).
Spacing float64
}
TextRun is a stretch of text in one style. Font names the typeface (an embedded one, see Presentation.Fonts, or one the viewer has); Size is in points. Link, when set, is an http, https or mailto URL the run opens.