docx

package module
v0.0.0-...-8c7ba35 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: MIT Imports: 5 Imported by: 0

README

docx

Introduction

docx is a simple library to creating DOCX file in Go.

Getting Started

Install

Go modules supported

go get github.com/ajbzd/go-docx

Import:

import "github.com/ajbzd/go-docx"
Usage

Example:

package main

import (
	"github.com/ajbzd/go-docx"
)

func main() {
	f := docx.NewFile()
	// add new paragraph
	para := f.AddParagraph()
	// add text
	para.AddText("test")

	para.AddText("test font size").Size(22)
	para.AddText("test color").Color("808080")
	para.AddText("test font size and color").Size(22).Color("121212")

	nextPara := f.AddParagraph()
	nextPara.AddLink("google", `http://google.com`)

	f.Save("./test.docx")
}

Documentation

Index

Constants

View Source
const (
	XMLNS_W = `http://schemas.openxmlformats.org/wordprocessingml/2006/main`
	XMLNS_R = `http://schemas.openxmlformats.org/officeDocument/2006/relationships`
)
View Source
const (
	JUSTIFY_START      = "start"
	JUSTIFY_END        = "end"
	JUSTIFY_BOTH       = "both"
	JUSTIFY_CENTER     = "center"
	JUSTIFY_DISTRIBUTE = "distribute"
	CM                 = 567
	INCH               = 1440
)
View Source
const (
	XMLNS         = `http://schemas.openxmlformats.org/package/2006/relationships`
	REL_HYPERLINK = `http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink`

	REL_TARGETMODE = "External"
)
View Source
const (
	BLACK                                   = "000000"
	TABLE_BORDER_VAL_SINGLE                 = "single"                 // a single line
	TABLE_BORDER_VAL_DASHDOTSTROKED         = "dashDotStroked"         // a line with a series of alternating thin and thick strokes
	TABLE_BORDER_VAL_DASHED                 = "dashed"                 // a dashed line
	TABLE_BORDER_VAL_DASHSMALLGAP           = "dashSmallGap"           // a dashed line with small gaps
	TABLE_BORDER_VAL_DOTDASH                = "dotDash"                // a line with alternating dots and dashes
	TABLE_BORDER_VAL_DOTDOTDASH             = "dotDotDash"             // a line with a repeating dot - dot - dash sequence
	TABLE_BORDER_VAL_DOTTED                 = "dotted"                 // a dotted line
	TABLE_BORDER_VAL_DOUBLE                 = "double"                 // a double line
	TABLE_BORDER_VAL_DOUBLEWAVE             = "doubleWave"             // a double wavy line
	TABLE_BORDER_VAL_INSET                  = "inset"                  // an inset set of lines
	TABLE_BORDER_VAL_NIL                    = "nil"                    // no border
	TABLE_BORDER_VAL_NONE                   = "none"                   // no border
	TABLE_BORDER_VAL_OUTSET                 = "outset"                 // an outset set of lines
	TABLE_BORDER_VAL_THICK                  = "thick"                  // a single line
	TABLE_BORDER_VAL_THICKTHINLARGEGAP      = "thickThinLargeGap"      // a thick line contained within a thin line with a large-sized intermediate gap
	TABLE_BORDER_VAL_THICKTHINMEDIUMGAP     = "thickThinMediumGap"     // a thick line contained within a thin line with a medium-sized intermediate gap
	TABLE_BORDER_VAL_THICKTHINSMALLGAP      = "thickThinSmallGap"      // a thick line contained within a thin line with a small-sized intermediate gap
	TABLE_BORDER_VAL_THINTHICKLARGEGAP      = "thinThickLargeGap"      // a thin line contained within a thick line with a large-sized intermediate gap
	TABLE_BORDER_VAL_THINTHICKMEDIUMGAP     = "thinThickMediumGap"     // a thin line contained within a thick line with a medium-sized intermediate gap
	TABLE_BORDER_VAL_THINTHICKSMALLGAP      = "thinThickSmallGap"      // a thin line contained within a thick line with a small-sized intermediate gap
	TABLE_BORDER_VAL_THINTHICKTHINLARGEGAP  = "thinThickThinLargeGap"  // a thin-thick-thin line with a large gap
	TABLE_BORDER_VAL_THINTHICKTHINMEDIUMGAP = "thinThickThinMediumGap" // a thin-thick-thin line with a medium gap
	TABLE_BORDER_VAL_THINTHICKTHINSMALLGAP  = "thinThickThinSmallGap"  // a thin-thick-thin line with a small gap
	TABLE_BORDER_VAL_THREEDEEMBOSS          = "threeDEmboss"           // a three-staged gradient line, getting darker towards the paragraph
	TABLE_BORDER_VAL_THREEDEENGRAVE         = "threeDEngrave"          // a three-staged gradient like, getting darker away from the paragraph
	TABLE_BORDER_VAL_TRIPLE                 = "triple"                 // a triple line
	TABLE_BORDER_VAL_WAVE                   = "wave"                   // a wavy line
	TABLE_LAYOUT_TYPE_FIXED                 = "fixed"                  // fixed cell width
	TABLE_LAYOUT_TYPE_AUTO                  = "auto"                   // auto cell width

	CELL_VERTICAL_ALIGN_TOP    = "top"    // align content to the top of cell vertically
	CELL_VERTICAL_ALIGN_BOTTOM = "bottom" // align content to the bottom of cell vertically
	CELL_VERTICAL_ALIGN_CENTER = "center" // align content to the center of cell vertically
)
View Source
const (
	TEMP_REL = `` /* 601-byte string literal not displayed */

	TEMP_DOCPROPS_APP  = `` /* 276-byte string literal not displayed */
	TEMP_DOCPROPS_CORE = `` /* 363-byte string literal not displayed */
	TEMP_CONTENT       = `` /* 934-byte string literal not displayed */

	TEMP_WORD_STYLE = `` /* 1743-byte string literal not displayed */

	TEMP_WORD_THEME_THEME = `` /* 9765-byte string literal not displayed */

)
View Source
const (
	HYPERLINK_STYLE = "a1"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Body

type Body struct {
	XMLName xml.Name `xml:"w:body"`
	Content []interface{}
}

type Bold

type Bold struct {
	XMLName xml.Name `xml:"w:b"`
	Val     bool     `xml:"w:val,attr"`
}

type Cell

type Cell struct {
	XMLName    xml.Name `xml:"w:tc"`
	Data       []interface{}
	Properties *CellProperties
	Paragraph  *Paragraph
	// contains filtered or unexported fields
}

func (*Cell) AddText

func (c *Cell) AddText(text string) *Run

AddText is adding text to cell

type CellProperties

type CellProperties struct {
	XMLName xml.Name `xml:"w:tcPr"`
	Data    []interface{}
}

func (*CellProperties) VerticalAlignment

func (cp *CellProperties) VerticalAlignment(align string)

CELL_VERTICAL_ALIGN_TOP align content to the top of cell vertically CELL_VERTICAL_ALIGN_CENTER align content to the bottom of cell vertically CELL_VERTICAL_ALIGN_BOTTM align content to the center of cell vertically

type CellVerticalAlignment

type CellVerticalAlignment struct {
	XMLName xml.Name `xml:"w:vAlign"`
	Val     string   `xml:"w:val,attr"`
}

type Color

type Color struct {
	XMLName xml.Name `xml:"w:color"`
	Val     string   `xml:"w:val,attr"`
}

type DocRelation

type DocRelation struct {
	XMLName      xml.Name        `xml:"Relationships"`
	Xmlns        string          `xml:"xmlns,attr"`
	Relationship []*RelationShip `xml:"Relationship"`
}

type Document

type Document struct {
	XMLName xml.Name `xml:"w:document"`
	XMLW    string   `xml:"xmlns:w,attr"`
	XMLR    string   `xml:"xmlns:r,attr"`
	Body    *Body
}

type File

type File struct {
	Document    Document
	DocRelation DocRelation
	// contains filtered or unexported fields
}

func NewFile

func NewFile() *File

func (*File) AddParagraph

func (f *File) AddParagraph() *Paragraph

AddParagraph add new paragraph

func (*File) AddTable

func (f *File) AddTable() *Table

AddTable is adding table to document

func (*File) Save

func (f *File) Save(path string) (err error)

Save save file to path

func (*File) Write

func (f *File) Write(writer io.Writer) (err error)

type Font

type Font struct {
	XMLName xml.Name `xml:"w:rFonts"`
	Ascii   string   `xml:"w:ascii,attr"`
	HAnsi   string   `xml:"w:hAnsi,attr"`
	Cs      string   `xml:"w:cs,attr"`
}
type Hyperlink struct {
	XMLName xml.Name `xml:"w:hyperlink"`
	ID      string   `xml:"r:id,attr"`
	Run     Run
}

type Indentation

type Indentation struct {
	XMLName xml.Name `xml:"w:ind"`
	Left    int      `xml:"w:left,attr"`
	Right   int      `xml:"w:right,attr"`
}

type Justification

type Justification struct {
	XMLName xml.Name `xml:"w:jc"`
	Val     string   `xml:"w:val,attr"`
}

type KeepLines

type KeepLines struct {
	XMLName xml.Name `xml:"w:keepLines"`
}

type LineBreak

type LineBreak struct {
	XMLName xml.Name `xml:"w:br"`
	Type    string   `xml:"w:type,attr"`
}

type PageBreakBefore

type PageBreakBefore struct {
	XMLName xml.Name `xml:"w:pageBreakBefore"`
	Val     string   `xml:"w:val,attr"`
}

type PageMargin

type PageMargin struct {
	XMLName xml.Name `xml:"w:pgMar"`
	Header  int      `xml:"w:header,attr"`
	Bottom  int      `xml:"w:bottom,attr"`
	Top     int      `xml:"w:top,attr"`
	Right   int      `xml:"w:right,attr"`
	Left    int      `xml:"w:left,attr"`
	Footer  int      `xml:"w:footer,attr"`
	Gutter  int      `xml:"w:gutter,attr"`
}

type Paragraph

type Paragraph struct {
	XMLName    xml.Name `xml:"w:p"`
	Data       []interface{}
	Properties *ParagraphProperties
	// contains filtered or unexported fields
}
func (p *Paragraph) AddLink(text string, link string) *Hyperlink

AddLink add hyperlink to paragraph

func (*Paragraph) AddNewLine

func (p *Paragraph) AddNewLine()

AddNewLine adds break with type textWrapping, newline after previous

func (*Paragraph) AddNewPage

func (p *Paragraph) AddNewPage()

AddNewPage adds break with type page, new page after previous

func (*Paragraph) AddText

func (p *Paragraph) AddText(text string) *Run

AddText add text to paragraph

type ParagraphProperties

type ParagraphProperties struct {
	XMLName xml.Name `xml:"w:pPr"`
	Data    []interface{}
}

func (*ParagraphProperties) Indentation

func (prp *ParagraphProperties) Indentation(left, right int)

Indentation takes left and right indentation for paragraph in twips There is constant CM which is equal to 567 twips per centimeter There is constant INCH which is equal to 1440 twips per inch

func (*ParagraphProperties) Justification

func (prp *ParagraphProperties) Justification(justificaiton string)

Justification takes justificaiton type Possible values are in constants: JUSTIFY_START align to the left JUSTIFY_END align to the right JUSTIFY_BOTH align to both sides with regular spaces JUSTIFY_DISTRIBUTE align to both sides with wider spaces JUSTIFY_CENTER align to the center of line

func (*ParagraphProperties) KeepLines

func (prp *ParagraphProperties) KeepLines()

KeepLines prevents pagebreak within a paragraph

func (*ParagraphProperties) PageBreakBefore

func (prp *ParagraphProperties) PageBreakBefore(val string)

Define Pagebreak before paragraph

func (*ParagraphProperties) Spacing

func (prp *ParagraphProperties) Spacing(before, after int)

Spacing sets the space before and after paragraph

type RelationShip

type RelationShip struct {
	XMLName    xml.Name `xml:"Relationship"`
	ID         string   `xml:"Id,attr"`
	Type       string   `xml:"Type,attr"`
	Target     string   `xml:"Target,attr"`
	TargetMode string   `xml:"TargetMode,attr,omitempty"`
}

type Row

type Row struct {
	XMLName xml.Name `xml:"w:tr"`
	Data    []interface{}
	// contains filtered or unexported fields
}

func (*Row) AddCell

func (r *Row) AddCell(width int) *Cell

AddCell is adding cell to row, takes width in twips

type RowProperties

type RowProperties struct {
	XMLName xml.Name `xml:"w:trPr"`
	Data    []interface{}
	// contains filtered or unexported fields
}

type Run

type Run struct {
	XMLName       xml.Name       `xml:"w:r"`
	RunProperties *RunProperties `xml:"w:rPr,omitempty"`
	InstrText     string         `xml:"w:instrText,omitempty"`
	Text          *Text
}

func (*Run) Bold

func (r *Run) Bold(bold bool) *Run

Bold set run bold

func (*Run) Color

func (r *Run) Color(color string) *Run

Color set run color

func (*Run) Font

func (r *Run) Font(font string) *Run

Font set run font

func (*Run) Size

func (r *Run) Size(size int) *Run

Size set run size

type RunProperties

type RunProperties struct {
	XMLName  xml.Name  `xml:"w:rPr"`
	Color    *Color    `xml:"w:color,omitempty"`
	Size     *Size     `xml:"w:sz,omitempty"`
	Bold     *Bold     `xml:"w:b,omitempty"`
	Font     *Font     `xml:"w:rFonts,omitempty"`
	RunStyle *RunStyle `xml:"w:rStyle,omitempty"`
}

type RunStyle

type RunStyle struct {
	XMLName xml.Name `xml:"w:rStyle"`
	Val     string   `xml:"w:val,attr"`
}

type SectionProperties

type SectionProperties struct {
	XMLName    xml.Name `xml:"w:sectPr"`
	PageMargin *PageMargin
}

type Size

type Size struct {
	XMLName xml.Name `xml:"w:sz"`
	Val     int      `xml:"w:val,attr"`
}

type Spacing

type Spacing struct {
	XMLName xml.Name `xml:"w:spacing"`
	Before  int      `xml:"w:before,attr"`
	After   int      `xml:"w:after,attr"`
}

type Table

type Table struct {
	XMLName xml.Name `xml:"w:tbl"`
	Data    []interface{}

	Properties *TableProperties
	// contains filtered or unexported fields
}

func (*Table) AddRow

func (t *Table) AddRow() *Row

AddRow is adding row to table

type TableBorders

type TableBorders struct {
	XMLName xml.Name `xml:"w:tblBorders"`
	Top     *TableBordersTop
	Bottom  *TableBordersBottom
	Start   *TableBordersStart
	End     *TableBordersEnd
	InsideV *TableBordersInsideV
	InsideH *TableBordersInsideH
}

type TableBordersAttributes

type TableBordersAttributes struct {
	Color  string `xml:"w:color,attr"`
	Shadow string `xml:"w:shadow,attr"`
	Space  int    `xml:"w:space,attr"`
	Size   int    `xml:"w:sz,attr"`
	Val    string `xml:"w:val,attr"`
}

type TableBordersBottom

type TableBordersBottom struct {
	XMLName xml.Name `xml:"w:bottom"`
	TableBordersAttributes
}

TableBordersBottom specifies the border displayed at the bottom of a table.

type TableBordersEnd

type TableBordersEnd struct {
	XMLName xml.Name `xml:"w:end"`
	TableBordersAttributes
}

TableBordersEnd specifies the border displayed on the right for left-to-right tables and left for right-to-left tables

type TableBordersInsideH

type TableBordersInsideH struct {
	XMLName xml.Name `xml:"w:insideH"`
	TableBordersAttributes
}

TableBordersInsideH specifies the border displayed on all inside horizontal table cell borders.

type TableBordersInsideV

type TableBordersInsideV struct {
	XMLName xml.Name `xml:"w:insideV"`
	TableBordersAttributes
}

TableBordersInsideV specifies the border displayed on all inside vertical table cell borders.

type TableBordersStart

type TableBordersStart struct {
	XMLName xml.Name `xml:"w:start"`
	TableBordersAttributes
}

TableBordersStart specifies the border displayed to the left for left-to-right tables and right for right-to-left tables.

type TableBordersTop

type TableBordersTop struct {
	XMLName xml.Name `xml:"w:top"`
	TableBordersAttributes
}

TableBordersTop specifies the border displayed at the top of a table.

type TableCellMargin

type TableCellMargin struct {
	XMLName xml.Name `xml:"w:tblCellMar"`
	Top     *TableCellMarginTop
	Bottom  *TableCellMarginBottom
	Start   *TableCellMarginStart
	End     *TableCellMarginEnd
}

type TableCellMarginAttributes

type TableCellMarginAttributes struct {
	Width int    `xml:"w:w,attr"`
	Type  string `xml:"w:type,attr"`
}

type TableCellMarginBottom

type TableCellMarginBottom struct {
	XMLName xml.Name `xml:"w:bottom"`
	TableCellMarginAttributes
}

type TableCellMarginEnd

type TableCellMarginEnd struct {
	XMLName xml.Name `xml:"w:end"`
	TableCellMarginAttributes
}

type TableCellMarginStart

type TableCellMarginStart struct {
	XMLName xml.Name `xml:"w:start"`
	TableCellMarginAttributes
}

type TableCellMarginTop

type TableCellMarginTop struct {
	XMLName xml.Name `xml:"w:top"`
	TableCellMarginAttributes
}

type TableGrid

type TableGrid struct {
	XMLName xml.Name `xml:"w:tblGrid"`
	Data    []interface{}
}

type TableLayout

type TableLayout struct {
	XMLName xml.Name `xml:"w:tblLayout"`
	Type    string   `xml:"w:type,attr"`
}

type TableProperties

type TableProperties struct {
	XMLName    xml.Name `xml:"w:tblPr"`
	Width      *TableWidth
	Borders    *TableBorders
	Layout     *TableLayout
	CellMargin *TableCellMargin
}

type TableWidth

type TableWidth struct {
	XMLName xml.Name `xml:"w:tblW"`
	Width   int      `xml:"w:w,attr"`
	Type    string   `xml:"w:type,attr"`
}

type Text

type Text struct {
	XMLName  xml.Name `xml:"w:t"`
	XMLSpace string   `xml:"xml:space,attr,omitempty"`
	Text     string   `xml:",chardata"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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