91 lines
1.7 KiB
Go
91 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/gingfrederik/docx"
|
|
)
|
|
|
|
var docPath = flag.String("o", "", "Path to the DOC output file")
|
|
var txtPath = flag.String("f", "", "Path to the text input file")
|
|
|
|
func usage() {
|
|
fmt.Printf(`%s: -f=<Text Input File> -o=<DOC Output File>
|
|
|
|
`,
|
|
os.Args[0])
|
|
}
|
|
|
|
/*
|
|
func generateXLSXFromCSV(csvPath string, XLSXPath string, delimiter string) error {
|
|
csvFile, err := os.Open(csvPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer csvFile.Close()
|
|
reader := csv.NewReader(csvFile)
|
|
if len(delimiter) > 0 {
|
|
reader.Comma = rune(delimiter[0])
|
|
} else {
|
|
reader.Comma = rune(';')
|
|
}
|
|
xlsxFile := xlsx.NewFile()
|
|
sheet, err := xlsxFile.AddSheet(csvPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fields, err := reader.Read()
|
|
for err == nil {
|
|
row := sheet.AddRow()
|
|
for _, field := range fields {
|
|
cell := row.AddCell()
|
|
cell.Value = field
|
|
}
|
|
fields, err = reader.Read()
|
|
}
|
|
if err != nil {
|
|
fmt.Printf(err.Error())
|
|
}
|
|
return xlsxFile.Save(XLSXPath)
|
|
}*/
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
if len(os.Args) < 2 {
|
|
usage()
|
|
return
|
|
}
|
|
flag.Parse()
|
|
err := generateDOCFromText(*txtPath, *docPath)
|
|
if err != nil {
|
|
fmt.Printf(err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
func generateDOCFromText(txtPath string, docPath string) error {
|
|
|
|
txtFile, err := os.Open(txtPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open text file: %v", err)
|
|
}
|
|
defer txtFile.Close()
|
|
b, err := io.ReadAll(txtFile)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read text file: %v", err)
|
|
}
|
|
|
|
docFile := docx.NewFile()
|
|
para := docFile.AddParagraph()
|
|
para.AddText(string(b)).Size(9)
|
|
err = docFile.Save(docPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to save doc file: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|