mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-12 03:20:23 +08:00
feat: chage template name (due2 #17)
This commit is contained in:
@@ -67,7 +67,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
w := docx.NewA4()
|
w := docx.New().WithDefaultTheme()
|
||||||
// add new paragraph
|
// add new paragraph
|
||||||
para1 := w.AddParagraph()
|
para1 := w.AddParagraph()
|
||||||
// add text
|
// add text
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func main() {
|
|||||||
if !*analyzeOnly {
|
if !*analyzeOnly {
|
||||||
fmt.Printf("Preparing new document to write at %s\n", *fileLocation)
|
fmt.Printf("Preparing new document to write at %s\n", *fileLocation)
|
||||||
|
|
||||||
w = docx.NewA4()
|
w = docx.New().WithDefaultTheme()
|
||||||
// add new paragraph
|
// add new paragraph
|
||||||
para1 := w.AddParagraph().Justification("distribute")
|
para1 := w.AddParagraph().Justification("distribute")
|
||||||
r, err := para1.AddAnchorDrawingFrom("testdata/fumiama.JPG")
|
r, err := para1.AddAnchorDrawingFrom("testdata/fumiama.JPG")
|
||||||
@@ -253,7 +253,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
newFile := docx.NewA4()
|
newFile := docx.New().WithDefaultTheme()
|
||||||
for i := 0; i < int(*dupnum); i++ {
|
for i := 0; i < int(*dupnum); i++ {
|
||||||
newFile.AppendFile(doc)
|
newFile.AppendFile(doc)
|
||||||
}
|
}
|
||||||
|
|||||||
13
docx.go
13
docx.go
@@ -55,10 +55,10 @@ type Docx struct {
|
|||||||
io.WriterTo
|
io.WriterTo
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewA4 generates a new empty A4 docx file that we can manipulate and
|
// New generates a new empty docx file that we can manipulate and
|
||||||
// later on, save
|
// later on, save
|
||||||
func NewA4() *Docx {
|
func New() *Docx {
|
||||||
return newEmptyA4File()
|
return newEmptyFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse generates a new docx file in memory from a reader
|
// Parse generates a new docx file in memory from a reader
|
||||||
@@ -160,10 +160,3 @@ func (f *Docx) WriteTo(writer io.Writer) (_ int64, err error) {
|
|||||||
func (f *Docx) Read(_ []byte) (int, error) {
|
func (f *Docx) Read(_ []byte) (int, error) {
|
||||||
return 0, os.ErrInvalid
|
return 0, os.ErrInvalid
|
||||||
}
|
}
|
||||||
|
|
||||||
// UseTemplate will replace template files
|
|
||||||
func (f *Docx) UseTemplate(template string, tmpfslst []string, tmplfs fs.FS) {
|
|
||||||
f.template = template
|
|
||||||
f.tmplfs = tmplfs
|
|
||||||
f.tmpfslst = tmpfslst
|
|
||||||
}
|
|
||||||
|
|||||||
4
empty.go
4
empty.go
@@ -24,7 +24,7 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newEmptyA4File() *Docx {
|
func newEmptyFile() *Docx {
|
||||||
docx := &Docx{
|
docx := &Docx{
|
||||||
Document: Document{
|
Document: Document{
|
||||||
XMLName: xml.Name{
|
XMLName: xml.Name{
|
||||||
@@ -68,8 +68,6 @@ func newEmptyA4File() *Docx {
|
|||||||
mediaNameIdx: make(map[string]int, 64),
|
mediaNameIdx: make(map[string]int, 64),
|
||||||
rID: 3,
|
rID: 3,
|
||||||
slowIDs: make(map[string]uintptr, 64),
|
slowIDs: make(map[string]uintptr, 64),
|
||||||
template: "a4",
|
|
||||||
tmpfslst: A4TemplateFilesList,
|
|
||||||
}
|
}
|
||||||
docx.Document.Body.file = docx
|
docx.Document.Body.file = docx
|
||||||
return docx
|
return docx
|
||||||
|
|||||||
6
fs.go
6
fs.go
@@ -25,11 +25,11 @@ import "embed"
|
|||||||
var (
|
var (
|
||||||
// TemplateXMLFS stores template docx files
|
// TemplateXMLFS stores template docx files
|
||||||
//go:embed xml
|
//go:embed xml
|
||||||
//go:embed xml/a4/_rels/*
|
//go:embed xml/default/_rels/*
|
||||||
TemplateXMLFS embed.FS
|
TemplateXMLFS embed.FS
|
||||||
|
|
||||||
// A4TemplateFilesList is the files list under TemplateXMLFS/xml/a4
|
// DefaultTemplateFilesList is the files list under TemplateXMLFS/xml/default
|
||||||
A4TemplateFilesList = []string{
|
DefaultTemplateFilesList = []string{
|
||||||
"_rels/.rels",
|
"_rels/.rels",
|
||||||
"docProps/app.xml",
|
"docProps/app.xml",
|
||||||
"docProps/core.xml",
|
"docProps/core.xml",
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestDrawingStructure(t *testing.T) {
|
func TestDrawingStructure(t *testing.T) {
|
||||||
w := NewA4()
|
w := New().WithDefaultTheme()
|
||||||
// add new paragraph
|
// add new paragraph
|
||||||
para1 := w.AddParagraph()
|
para1 := w.AddParagraph()
|
||||||
// add text
|
// add text
|
||||||
@@ -66,7 +66,7 @@ func TestDrawingStructure(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
w = NewA4()
|
w = New().WithDefaultTheme()
|
||||||
err = xml.NewDecoder(f).Decode(&w.Document)
|
err = xml.NewDecoder(f).Decode(&w.Document)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestShapeStructure(t *testing.T) {
|
func TestShapeStructure(t *testing.T) {
|
||||||
w := NewA4()
|
w := New().WithDefaultTheme()
|
||||||
// add new paragraph
|
// add new paragraph
|
||||||
para1 := w.AddParagraph()
|
para1 := w.AddParagraph()
|
||||||
// add text
|
// add text
|
||||||
@@ -69,7 +69,7 @@ func TestShapeStructure(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
w = NewA4()
|
w = New().WithDefaultTheme()
|
||||||
err = xml.NewDecoder(f).Decode(&w.Document)
|
err = xml.NewDecoder(f).Decode(&w.Document)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestTableStructure(t *testing.T) {
|
func TestTableStructure(t *testing.T) {
|
||||||
w := NewA4()
|
w := New().WithDefaultTheme()
|
||||||
// add new paragraph
|
// add new paragraph
|
||||||
para1 := w.AddParagraph()
|
para1 := w.AddParagraph()
|
||||||
// add text
|
// add text
|
||||||
@@ -63,7 +63,7 @@ func TestTableStructure(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
w = NewA4()
|
w = New().WithDefaultTheme()
|
||||||
err = xml.NewDecoder(f).Decode(&w.Document)
|
err = xml.NewDecoder(f).Decode(&w.Document)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
36
theme.go
Normal file
36
theme.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2020 gingfrederik
|
||||||
|
Copyright (c) 2021 Gonzalo Fernandez-Victorio
|
||||||
|
Copyright (c) 2021 Basement Crowd Ltd (https://www.basementcrowd.com)
|
||||||
|
Copyright (c) 2024 Fumiama Minamoto (源文雨)
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Affero General Public License as published
|
||||||
|
by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package docx
|
||||||
|
|
||||||
|
import "io/fs"
|
||||||
|
|
||||||
|
// UseTemplate will replace template files
|
||||||
|
func (f *Docx) UseTemplate(template string, tmpfslst []string, tmplfs fs.FS) *Docx {
|
||||||
|
f.template = template
|
||||||
|
f.tmplfs = tmplfs
|
||||||
|
f.tmpfslst = tmpfslst
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithDefaultTheme use default theme embeded
|
||||||
|
func (f *Docx) WithDefaultTheme() *Docx {
|
||||||
|
return f.UseTemplate("default", DefaultTemplateFilesList, TemplateXMLFS)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user