en Quick Start - ZeroHawkeye/wordZero GitHub Wiki

Quick Start

Welcome to WordZero! This chapter will guide you through getting started quickly and creating your first Word document.

📦 Installation

Prerequisites

  • Go 1.19 or higher
  • Basic Go programming knowledge

Installing WordZero

Install using Go modules:

go mod init your-project-name
go get github.com/ZeroHawkeye/wordZero

🏗️ Core Concepts

Before we start coding, let's understand WordZero's core concepts:

Document

The Document is WordZero's core object, representing a complete Word document.

Paragraph

A Paragraph is the basic text container in a document, which can contain text, formatting, and styles.

Style

WordZero provides 18 predefined styles, including headings, body text, quotes, and more.

Table

Tables are used to organize structured data, with support for styling and cell operations.

Image

WordZero supports inserting images in documents, including PNG, JPEG, GIF formats, with various layout and positioning controls.

🚀 First Example

Let's create a simple Word document:

package main

import (
    "fmt"
    "log"
    
    "github.com/ZeroHawkeye/wordZero/pkg/document"
    "github.com/ZeroHawkeye/wordZero/pkg/style"
)

func main() {
    // 1. Create a new document
    doc := document.New()
    
    // 2. Add title
    title := doc.AddParagraph("My First WordZero Document")
    title.SetStyle(style.StyleTitle)
    
    // 3. Add subtitle
    subtitle := doc.AddParagraph("Creating Word Documents with Go")
    subtitle.SetStyle(style.StyleSubtitle)
    
    // 4. Add body paragraph
    content := doc.AddParagraph("This is my first Word document created with WordZero. WordZero is an easy-to-use Go library for Word document manipulation.")
    content.SetStyle(style.StyleNormal)
    
    // 5. Save the document
    err := doc.SaveToFile("my-first-document.docx")
    if err != nil {
        log.Fatalf("Failed to save document: %v", err)
    }
    
    fmt.Println("Document created successfully!")
}

📁 Recommended Project Structure

When creating a new project, we recommend using the following structure:

your-project/
├── main.go                 # Main program file
├── go.mod                  # Go module file
├── docs/                   # Generated documents directory
│   └── *.docx             # Word document files
└── README.md              # Project description

🔧 Basic Operation Flow

The basic workflow for using WordZero:

  1. Create Document: Use document.New() to create a new document
  2. Add Content: Use AddParagraph() to add paragraphs
  3. Set Styles: Use SetStyle() to apply styles
  4. Save Document: Use SaveToFile() to save to file

💡 Common Styles

WordZero provides the following commonly used predefined styles:

  • style.StyleTitle - Document title
  • style.StyleSubtitle - Subtitle
  • style.StyleHeading1 - Level 1 heading
  • style.StyleHeading2 - Level 2 heading
  • style.StyleHeading3 - Level 3 heading
  • style.StyleNormal - Body text
  • style.StyleQuote - Quote

⚠️ Important Notes

  1. File Extension: Ensure you use the .docx extension when saving files
  2. Error Handling: Always check for errors returned by functions
  3. File Path: Ensure the save path exists and has write permissions

🎯 Practice Exercises

Try modifying the example above:

  1. Add more paragraphs
  2. Use different styles
  3. Change the document title
  4. Save to a different filename
  5. Add images to the document
  6. Try different image positions and sizes

Next Steps

Congratulations! You have successfully created your first WordZero document.

Next, you can continue learning:


Start your WordZero journey!