Documentation
¶
Index ¶
- Constants
- Variables
- func ExtractText(sourceFilePath string) (map[string][]string, error)
- func ExtractTextBytes(sourceBytes []byte) (map[string][]string, error)
- func ModifyText(sourceFilePath string, replace Replacer, targetFilePath string) error
- func ModifyTextBytes(sourceBytes []byte, replace Replacer) ([]byte, error)
- func ReaderToZipReader(r io.Reader) (*zip.Reader, error)
- func RegisterTplFunction(name string, function any)
- type Replacer
Constants ¶
const ( AUTHOR = "Xavier Gandillot" DESCRIPTION = "A simple library to modify Microsoft Word .docx documents with go templates" NAME = "mydocx" VERSION = "0.2.3" COPYRIGHT = "(c) Xavier Gandillot 2024" NAMESPACE = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" )
Variables ¶
var ( // if true, verbose information will be printed to stdout VERBOSE = false // If set to true, paragraphs that become empty after replacement are removed (paragraphs that were initially empty, before replacement, are never removed). // If false, paragraphs that become empty are kept. // Use the functions {{removeEmpty}} or {{keepEmpty}} in the source word document to set this value. // You may also set this variable directly from code. // Default is true. REMOVE_EMPTY_PARAGRAPH bool = true )
Functions ¶
func ExtractText ¶
Extract text content from docx file for external processing. Returns a map from the container name (eg : word/footer1.xml) to a list of text contained in its paragraphs. This function is thread-safe. The verbose flag can be set to true to display information about the containers extracted.
func ExtractTextBytes ¶ added in v0.2.3
Same as ExtractText, but takes a byte array as input. This is useful for embedded use, when the docx file is already in memory. Returns a map from the container name (eg : word/footer1.xml) to a list of text contained in its paragraphs. This function is thread-safe. The verbose flag can be set to true to display information about the containers extracted.
func ModifyText ¶
All text from the sourceFile is modified by applying the Replacer. Before calling Replacer, the whole paragraph is collected as a single text, even if split on multiple runs. Replacer is called paragraph by paragraph. It is never called on empty paragraphs. If the Replacer is nil, text will be copied unmodified (but paragraph format WILL be extended from the start of paragraph, removing subsequent paragraph formatting ). If the targetFile name is empty, the sourceFile will be used, modification will be done in place.
func ModifyTextBytes ¶ added in v0.2.3
All text from the sourceBytes is modified by applying the Replacer. Before calling Replacer, the whole paragraph is collected as a single text, even if split on multiple runs. Replacer is called paragraph by paragraph. It is never called on empty paragraphs. If the Replacer is nil, text will be copied unmodified (but paragraph format WILL be extended from the start of paragraph, removing subsequent paragraph formatting).
func ReaderToZipReader ¶ added in v0.2.3
Creates a zipRedaer from an io.Reader
func RegisterTplFunction ¶ added in v0.2.1
Register a new function that will be available when parsing templates. Empty names or nil functions are ignored. Each function must have either a single return value, or two return values of which the second has type error. In that case, if the second (error) return value evaluates to non-nil during execution, execution terminates and Execute returns that error.
Types ¶
type Replacer ¶
A Replacer replaces a string with a list of modified string. It is provided the container name where replacement will occur ("word/document.xm", "word/footer1.xml", ...). Only documents, headers and footers will be submitted. If the returned slice is empty, the paragraph is removed (unless the REMOVE_EMPTY_PRAGRAPHS flag was unset) If the returned slice contains more than 1 element, new paragraphs are added, duplicated from the original paragraph. The strings will be xml-escaped later, the Replacer should not escape its results.
func NewTplReplacer ¶
Assume the source word document contains valid go template in each paragraph NewTplReplacer creates a replacer that will apply the provided content struct to the template in each paragraph. Container is ignored. The Replacer will behave as follows : * If initial paragraph was empty, it is left un changed. * If not empty, template is executed. * If the template execution result is empty, the paragraph is discarded. * If the template execution result is not empty, it is split around \n into lines and each line is added as a separate paragraph. (you may use the function {{nl}} to gererate new lines) * If an error occurs during template execution, an error message is added as the last paragraph of the result.