gopptx

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2026 License: BSD-3-Clause Imports: 16 Imported by: 0

README

gopptx logo

go.dev Licenses

GoPPTX

Introduction

GoPPTX is a library written in pure Go providing a set of functions that allow you to write to and read from PPTX files. Supports reading and writing presentation documents generated by Microsoft PowerPoint™ 2007 and later. Supports complex components by high compatibility, and provided streaming API for generating or reading data from a presentation with huge amounts of data. This library needs Go version 1.24.0 or later. The full docs can be seen using go's built-in documentation tool, or online at go.dev.

Basic Usage

Installation
go get github.com/kenny-not-dead/gopptx
Create presentation

The following constitutes the bare to create a presentation document.

package main

import (
    "fmt"

	"github.com/kenny-not-dead/gopptx"
)

func main() {
	f := gopptx.NewFile()
    defer func() {
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()

	_ ,err := f.NewSlide()
	if err != nil {
        fmt.Println(err)

		return
	}

    err := f.SaveAs("presentation.pptx")
	if err != nil {
        fmt.Println(err)

		return
	}
}
Reading presentation

The following constitutes the bare to read a presentation document.

package main

import (
    "fmt"

	"github.com/kenny-not-dead/gopptx"
)

func main() {
    f, err := excelize.OpenFile("presentation.pptx")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer func() {
        // Close the presentation.
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()
    // Get shapes from slide by given slude name.
	for _, slide := range f.Presentation.Slides.Slide {
		shapes, err := f.GetShapes(slide.ID)
		if err != nil {
			return
		}

		for _, shape := range shapes {
			fmt.Println(shape.TextBody)
		}
	}
}

Contributing

Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature or change. XML is compliant with part 1 of the 5th edition of the ECMA-376 Standard for Office Open XML.

Licenses

This program is under the terms of the BSD 3-Clause License. See https://opensource.org/licenses/BSD-3-Clause.

The PowerPoint logo is a trademark of Microsoft Corporation. This artwork is an adaptation.

Documentation

Index

Constants

View Source
const (
	MaxFilePathLength = 207
	StreamChunkSize   = 1 << 24
	UnzipSizeLimit    = 1000 << 24
)
View Source
const (
	ContentTypePresentationML                     = "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"
	ContentTypeSlideML                            = "application/vnd.openxmlformats-officedocument.presentationml.slide+xml"
	ContentTypeRelationships                      = "application/vnd.openxmlformats-package.relationships+xml"
	ContentTypeVBA                                = "application/vnd.ms-office.vbaProject"
	NameSpaceDrawingMLMain                        = "http://schemas.openxmlformats.org/drawingml/2006/main"
	NameSpaceExtendedProperties                   = "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
	NameSpaceXML                                  = "http://www.w3.org/XML/1998/namespace"
	SourceRelationshipCustomProperties            = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties"
	SourceRelationshipOfficeDocument              = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
	SourceRelationshipSlide                       = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide"
	StrictNameSpaceDocumentPropertiesVariantTypes = "http://purl.oclc.org/ooxml/officeDocument/docPropsVTypes"
	StrictNameSpaceDrawingMLMain                  = "http://purl.oclc.org/ooxml/drawingml/main"
	StrictNameSpaceExtendedProperties             = "http://purl.oclc.org/ooxml/officeDocument/extendedProperties"

	NameSpacePresentationMLMain       = "http://schemas.openxmlformats.org/presentationml/2006/main"
	StrictNameSpacePresentationMLMain = "http://purl.oclc.org/ooxml/presentationml/main"
)
View Source
const (
	MaxFieldLength = 255
)

Variables

View Source
var (
	// ErrPasswordLengthInvalid defined the error message on invalid password
	// length.
	ErrPasswordLengthInvalid = errors.New("password length invalid")
	// ErrOptionsUnzipSizeLimit defined the error message for receiving
	// invalid UnzipSizeLimit and UnzipXMLSizeLimit.
	ErrOptionsUnzipSizeLimit = errors.New("the value of UnzipSizeLimit should be greater than or equal to UnzipXMLSizeLimit")
	// ErrMaxFilePathLength defined the error message on receive the file path
	// length overflow.
	ErrMaxFilePathLength = fmt.Errorf("file path length exceeds maximum limit %d characters", MaxFilePathLength)
	// ErrSave defined the error message for saving file.
	ErrSave = errors.New("no path defined for file, consider File.WriteTo or File.Write")
	// ErrUnsupportedEncryptMechanism defined the error message on receive the blank slide name.
	ErrSlideNameBlank = errors.New("the slide name can not be blank")
	// ErrPresentationFileFormat defined the error message on receive an
	// unsupported presentation file format.
	ErrPresentationFileFormat = errors.New("unsupported presentation file format")
)
View Source
var (

	//go:embed templates/slide1.xml
	TemplateSlide string

	//go:embed templates/slide1-rels.xml
	TemplateSlideRels string
)
View Source
var (
	NameSpaceDocumentPropertiesVariantTypes = xml.Attr{Name: xml.Name{Local: "vt", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"}
	NameSpaceDrawing2016SVG                 = xml.Attr{Name: xml.Name{Local: "asvg", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/drawing/2016/SVG/main"}
	NameSpaceDrawingML                      = xml.Attr{Name: xml.Name{Local: "a", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/drawingml/2006/main"}
	NameSpaceDrawingMLA14                   = xml.Attr{Name: xml.Name{Local: "a14", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/drawing/2010/main"}
	NameSpaceDrawingMLChart                 = xml.Attr{Name: xml.Name{Local: "c", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/drawingml/2006/chart"}
	NameSpaceDrawingMLSlicer                = xml.Attr{Name: xml.Name{Local: "sle", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/drawing/2010/slicer"}
	NameSpaceDrawingMLSlicerX15             = xml.Attr{Name: xml.Name{Local: "sle15", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/drawing/2012/slicer"}
	SourceRelationship                      = xml.Attr{Name: xml.Name{Local: "r", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/officeDocument/2006/relationships"}
	SourceRelationshipCompatibility         = xml.Attr{Name: xml.Name{Local: "mc", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/markup-compatibility/2006"}
	NameSpacePresentationML                 = xml.Attr{Name: xml.Name{Local: "p", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/presentationml/2006/main"}
	NameSpacePowerPointR14                  = xml.Attr{Name: xml.Name{Local: "p14", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/powerpoint/2010/main"}
	NameSpacePowerPointR15                  = xml.Attr{Name: xml.Name{Local: "p15", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/powerpoint/2012/main"}
)

Functions

This section is empty.

Types

type AdjustValue

type AdjustValue struct {
	Name    string `xml:"name,attr"`
	Formula string `xml:"fmla,attr"`
}

type AdjustValueList added in v0.2.0

type AdjustValueList struct {
}

type BodyProperties

type BodyProperties struct {
	LIns      *int       `xml:"lIns,attr,omitempty"`
	RIns      *int       `xml:"rIns,attr,omitempty"`
	TIns      *int       `xml:"tIns,attr,omitempty"`
	BIns      *int       `xml:"bIns,attr,omitempty"`
	Anchor    *string    `xml:"anchor,attr,omitempty"`
	NoAutofit *NoAutofit `xml:"a:noAutofit,omitempty"`
}

type CommonNonVisualGroupShapeProperties

type CommonNonVisualGroupShapeProperties struct{}

type CommonNonVisualProperties

type CommonNonVisualProperties struct {
	ID   int    `xml:"id,attr"`
	Name string `xml:"name,attr"`
}

type CommonNonVisualShapeProperties

type CommonNonVisualShapeProperties struct {
	TxBox      *bool       `xml:"txBox,attr,omitempty"`
	ShapeLocks *ShapeLocks `xml:"a:spLocks,omitempty"`
}

type ErrSlideNotExist

type ErrSlideNotExist struct {
	SlideID int
}

ErrSlideNotExist defined an error of slide that does not exist.

func (ErrSlideNotExist) Error

func (err ErrSlideNotExist) Error() string

Error returns the error message on receiving the non existing slide name.

type Extents

type Extents struct {
	CX int `xml:"cx,attr"`
	CY int `xml:"cy,attr"`
}

type File

type File struct {
	CharsetReader func(charset string, input io.Reader) (rdr io.Reader, err error)
	ContentTypes  *contentTypes
	Path          string
	Pkg           sync.Map
	Presentation  *decodePresentation
	Relationships sync.Map
	Slide         sync.Map
	SlideCount    int
	Theme         *decodeTheme
	ZipWriter     func(io.Writer) ZipWriter
	// contains filtered or unexported fields
}

File define a populated slides file struct.

func NewFile added in v0.1.0

func NewFile(opts ...Options) *File

NewFile provides a function to create new file by default template. For example:

f := NewFile()

func OpenFile

func OpenFile(filename string, opts ...Options) (*File, error)

OpenFile take the name of a presentation file and returns a populated presentation file struct for it.

Close the file by Close function after opening the slides.

func OpenReader

func OpenReader(r io.Reader, opts ...Options) (*File, error)

OpenReader read data stream from io.Reader and return a populated presentation file.

func (*File) Close

func (f *File) Close() error

Close closes and cleanup the open temporary file for the presentation.

func (*File) GetGroupShapeProperties added in v0.0.2

func (f *File) GetGroupShapeProperties(slideID int) (*decodeGroupShapeProperties, error)

GetGroupShapeProperties provides a function to get group shape properties by given slide id.

func (*File) GetNonVisualGroupShapeProperties added in v0.0.2

func (f *File) GetNonVisualGroupShapeProperties(slideID int) (*decodeNonVisualGroupShapeProperties, error)

GetNonVisualGroupShapeProperties provides a function to get non visual group shape properties by given slide id.

func (*File) GetShapes

func (f *File) GetShapes(slideID int) ([]decodeShape, error)

GetShapes provides a function to get shapes by given slide id.

func (*File) NewSlide added in v0.2.0

func (f *File) NewSlide() (int, error)

NewSlide provides the function to create a new slide and returns the index of the slide in the presentation after it appended.

func (*File) ReadZipReader

func (f *File) ReadZipReader(r *zip.Reader) (map[string][]byte, int, error)

ReadZipReader extract presentation with given options.

func (*File) Save added in v0.1.0

func (f *File) Save(opts ...Options) error

Save provides a function to override the presentation with origin path.

func (*File) SaveAs added in v0.1.0

func (f *File) SaveAs(name string, opts ...Options) error

SaveAs provides a function to create or update to a presentation at the provided path.

func (*File) Write added in v0.1.0

func (f *File) Write(w io.Writer, opts ...Options) error

Write provides a function to write to an io.Writer.

func (*File) WriteTo added in v0.1.0

func (f *File) WriteTo(w io.Writer, opts ...Options) (int64, error)

WriteTo implements io.WriterTo to write the file.

func (*File) WriteToBuffer added in v0.1.0

func (f *File) WriteToBuffer() (*bytes.Buffer, error)

WriteToBuffer provides a function to get bytes.Buffer from the saved file, and it allocates space in memory. Be careful when the file size is large.

type GroupShapeProperties

type GroupShapeProperties struct {
	Xfrm *Xfrm `xml:"a:xfrm"`
}

type Latin

type Latin struct {
	Typeface string `xml:"typeface,attr"`
}

type Line

type Line struct {
	Width  *int    `xml:"w,attr,omitempty"`
	NoFill *noFill `xml:"a:noFill,omitempty"`
}

type LineSpacing

type LineSpacing struct {
	SpacingPercent *SpacingPercent `xml:"spcPct"`
}

type NoAutofit added in v0.2.0

type NoAutofit struct{}

type NonVisualGroupShapeProperties

type NonVisualGroupShapeProperties struct {
	CommonNonVisualProperties           *CommonNonVisualProperties           `xml:"p:cNvPr"`
	CommonNonVisualGroupShapeProperties *CommonNonVisualGroupShapeProperties `xml:"p:cNvGrpSpPr"`
	NonVisualProperties                 *NonVisualProperties                 `xml:"p:nvPr"`
}

type NonVisualProperties

type NonVisualProperties struct {
	Ph *Ph `xml:"p:ph,omitempty"`
}

type NonVisualShapeProperties

type NonVisualShapeProperties struct {
	CommonNonVisualProperties      *CommonNonVisualProperties      `xml:"p:cNvPr"`
	CommonNonVisualShapeProperties *CommonNonVisualShapeProperties `xml:"p:cNvSpPr"`
	NonVisualProperties            *NonVisualProperties            `xml:"p:nvPr"`
}

type Offset

type Offset struct {
	X int `xml:"x,attr"`
	Y int `xml:"y,attr"`
}

type Options

type Options struct {
	MaxCalcIterations uint
	Password          string
	RawCellValue      bool
	UnzipSizeLimit    int64
	UnzipXMLSizeLimit int64
	TmpDir            string
	ShortDatePattern  string
	LongDatePattern   string
	LongTimePattern   string
}

type Paragraph

type Paragraph struct {
	ParagraphProperties       *ParagraphProperties `xml:"a:pPr,omitempty"`
	Runs                      []Runs               `xml:"r"`
	EndParagraphRunProperties *RunProperties       `xml:"a:endParaRPr,omitempty"`
}

type ParagraphProperties

type ParagraphProperties struct {
	Indent      *int         `xml:"indent,attr,omitempty"`
	Align       *string      `xml:"algn,attr,omitempty"`
	LineSpacing *LineSpacing `xml:"lnSpc,omitempty"`
	BuNone      *struct{}    `xml:"buNone,omitempty"`
}

type Ph added in v0.2.0

type Ph struct {
	Type *string `xml:"type,attr,omitempty"`
}

type PresetGeometry

type PresetGeometry struct {
	Preset          string           `xml:"prst,attr"`
	AdjustValueList *AdjustValueList `xml:"a:avLst"`
}

type RunProperties

type RunProperties struct {
	Bold      *int       `xml:"b,attr,omitempty"`
	Lang      string     `xml:"lang,attr,omitempty"`
	Size      *int       `xml:"sz,attr,omitempty"`
	Space     *int       `xml:"spc,attr,omitempty"`
	Strike    string     `xml:"strike,attr,omitempty"`
	SolidFill *SolidFill `xml:"a:solidFill,omitempty"`
	Latin     *Latin     `xml:"a:latin,omitempty"`
}

type Runs

type Runs struct {
	RunProperties *decodeRunProperties `xml:"rPr,omitempty"`
	Text          string               `xml:"t"`
}

type Shape

type Shape struct {
	NonVisualShapeProperties *NonVisualShapeProperties `xml:"p:nvSpPr"`
	ShapeProperties          *ShapeProperties          `xml:"p:spPr"`
	TextBody                 *TextBody                 `xml:"p:txBody,omitempty"`
}

type ShapeLocks added in v0.2.0

type ShapeLocks struct {
	NoGroup *int `xml:"noGrp,attr,omitempty"`
}

type ShapeProperties

type ShapeProperties struct {
	Xfrm           *Xfrm           `xml:"a:xfrm"`
	PresetGeometry *PresetGeometry `xml:"a:prstGeom,omitempty"`
	NoFill         *noFill         `xml:"a:noFill,omitempty"`
	Ln             *Line           `xml:"a:ln,omitempty"`
}

type ShapeTree

type ShapeTree struct {
	NonVisualGroupShapeProperties *NonVisualGroupShapeProperties `xml:"p:nvGrpSpPr,omitempty"`
	GroupShapeProperties          *GroupShapeProperties          `xml:"p:grpSpPr,omitempty"`
	Shape                         []Shape                        `xml:"p:sp"`
}

type Slide

type Slide struct {
	XMLName                xml.Name          `xml:"p:sld"`
	XMLNSA                 string            `xml:"xmlns:a,attr"`
	XMLNSP                 string            `xml:"xmlns:p,attr"`
	XMLNSR                 string            `xml:"xmlns:r,attr"`
	XMLNSP14               string            `xml:"xmlns:p14,attr"`
	XMLNSP15               string            `xml:"xmlns:p15,attr"`
	XMLNSMC                string            `xml:"xmlns:mc,attr"`
	CommonSlideData        SlideData         `xml:"p:cSld"`
	AlternateContent       *alternateContent `xml:"mc:AlternateContent"`
	DecodeAlternateContent *innerXML         `xml:"http://schemas.openxmlformats.org/markup-compatibility/2006 AlternateContent"`
	// contains filtered or unexported fields
}

type SlideData

type SlideData struct {
	ShapeTree ShapeTree `xml:"p:spTree"`
}

type SolidFill

type SolidFill struct {
	SolidRGBColor *SolidRGBColor `xml:"a:srgbClr"`
}

type SolidRGBColor

type SolidRGBColor struct {
	Val string `xml:"val,attr"`
}

type SpacingPercent

type SpacingPercent struct {
	Val int `xml:"val,attr"`
}

type StreamWriter

type StreamWriter struct {
	SlideName string
	SlideID   int
	// contains filtered or unexported fields
}

type TextBody

type TextBody struct {
	BodyProperties *BodyProperties `xml:"a:bodyPr"`
	Paragraph      []Paragraph     `xml:"a:p"`
}

type Xfrm

type Xfrm struct {
	Offset       *Offset  `xml:"a:off"`
	Extents      *Extents `xml:"a:ext"`
	ChildOffset  *Offset  `xml:"a:chOff"`
	ChildExtents *Extents `xml:"a:chExt"`
}

type ZipWriter

type ZipWriter interface {
	Create(name string) (io.Writer, error)
	AddFS(fsys fs.FS) error
	Close() error
}

ZipWriter defines an interface for writing files to a ZIP archive. It provides methods to create new files within the archive, add files from a filesystem, and close the archive when writing is complete.

Jump to

Keyboard shortcuts

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