mydocx

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 9 Imported by: 1

README

GoDoc reference example

mydocx

Extract or Transform text content within a word document. Pure go, no external dependencies, no licensing fees.

Features

  • Extract paragraph text without modifying original word file
  • Replace paragraph text using a Replacer to transform paragraph content. A go template based Replacer is provided, but you may design your own.

Paragraphs

This library is designed to manage text within a paragraph, from a word docx model. It cannot create new paragraphs.

The template engine CANNOT be used across paragraph boundaries. You may NOT start an {{if ...}} in one paragraph and the balancing {{end}} in the next paragraph.

However, the Replacer CAN programatically request the suppression of its containing paragraph (useful if some paragraph only occur in certain cases, but you don't want empty lines).

Examples

See the testFiles directory.

Documentation

Index

Constants

View Source
const COPYRIGHT = "(c) Xavier Gandillot 2024"
View Source
const NAME = "mydocx"
View Source
const NAMESPACE = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
View Source
const VERSION = "0.1.6"

Variables

This section is empty.

Functions

func ExtractText

func ExtractText(sourceFilePath string) ([]string, error)

Extract text content from docx file for external processing. The slice of strings contains a string, possibly empty, for each paragraph.

func ModifyText

func ModifyText(sourceFilePath string, replace Replacer, targetFilePath string) error

All text from the sourceFile is modified by applying the replace function to it. Before applying the function, the whole paragraph is collected as a single text, even if split on multiple runs. Replace function is called paragraph by paragraph. No special assupmtion is made for empty paragraph. If the replace function is nil, text will be copied unmodified (but paragraph format WILL be extended from the start of paragraph, removing internal paragraph formatting !). If the targetFile name is empty, the sourceFile will be used, modification will be done in place.

Types

type Replacer

type Replacer func(original string) (replaced string, discard bool)

A replacer replaces a string with a modified string. It provides a flag that can trigger the removal of the entire paragraph. You may design your own Replacer, without relying on the go template engine (or using another one).

func NewTplReplacer

func NewTplReplacer(content any) Replacer

Assume the source word document contains valid template in each paragraph (CAUTION template logic cannot extend beyond paragraph boundaries !) NewTplReplacer creates a replacer that will apply the provided content struct to the template in each paragraph. By default, this Replacer will discard the entire paragraph if it was not initailly empty but becomes empty when executing the template. If an error occurs during template conversion, the text of the error is returned, together with the original text that triggered the error.

func NewTplReplacerNoDiscard

func NewTplReplacerNoDiscard(content any) Replacer

same as NewTplReplacer but will never discard empty paragraphs.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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