Skip to content

kenny-not-dead/gopptx

Repository files navigation

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)
        }
    }()

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

		return
	}

	title := gopptx.DecodeTextBody{
		BodyProperties: &gopptx.DecodeBodyProperties{
			LIns:      &value,
			RIns:      &value,
			TIns:      &value,
			BIns:      &value,
			Anchor:    &anchor,
			NoAutofit: &gopptx.NoAutofit{},
		},
		Paragraph: []gopptx.DecodeParagraph{
			{
				ParagraphProperties: &gopptx.ParagraphProperties{
					Indent: &value,
					Align:  &anchor,
				},
				Runs: []gopptx.DecodeRuns{
					{
						RunProperties: &gopptx.DecodeRunProperties{
							Bold: &value,
							Lang: "en-US",
							Size: &size,
							Strike: "noStrike",
							Space: &space,
							SolidFill: &gopptx.DecodeSolidFill{
								SolidRGBColor: &gopptx.SolidRGBColor{
									Val: "00008b",
								},
							},
							Latin: &gopptx.Latin{
								Typeface: "Arial",
							},
						},
						Text: "GOPPTX v0.3.1",
					},
				},
			},
		},
	}

	// 7 is default shapeID
	err = f.SetShapeTextBody(id, 7, title)
    if err != nil {
        fmt.Println(err)

		return
	}

	shapeProperties := gopptx.DecodeShapeProperties{
		Xfrm: &gopptx.DecodeXfrm{
			Offset: &gopptx.Offset{
				X: 1697760,
				Y: 1828800,
			},
			Extents: &gopptx.Extents{
				CX: 2417040,
				CY: 685800,
			},
		},
		PresetGeometry: &gopptx.DecodePresetGeometry{
			Preset: "rect",
		},
	}

	// 256 is default slideID, 8 is default shapeID
	err = f.DeleteShape(256, 8)
	if err != nil {
		fmt.Println("err: ", err)
	}

	_, err = f.CreateShape(256, shapeProperties, title)
	if err != nil {
		fmt.Println(err)
	}

    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 := gopptx.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.

About

gopptx provides functionality to create and manipulate PowerPoint (.pptx) files in Go, using the Office Open XML (ECMA-376) format.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages