Git Product home page Git Product logo

docx's Introduction

Simple Google Go (Golang) library for replacing text in Microsoft Word (.docx) file

The following constitutes the bare minimum required to replace text in DOCX document.

package main

import (
	"strconv"

	"github.com/nguyenthenguyen/docx"
)

func main() {
	// Read from docx file
	r, err := docx.ReadDocxFile("./TestDocument.docx")
	// Or read from memory
	// r, err := docx.ReadDocxFromMemory(data io.ReaderAt, size int64)

	// Or read from a filesystem object:
	// r, err := docx.ReadDocxFromFS(file string, fs fs.FS)

	if err != nil {
		panic(err)
	}
	docx1 := r.Editable()
	// Replace like https://golang.org/pkg/strings/#Replace
	docx1.Replace("old_1_1", "new_1_1", -1)
	docx1.Replace("old_1_2", "new_1_2", -1)
	docx1.ReplaceLink("http://example.com/", "https://github.com/nguyenthenguyen/docx", 1)
	docx1.ReplaceHeader("out with the old", "in with the new")
	docx1.ReplaceFooter("Change This Footer", "new footer")
	docx1.WriteToFile("./new_result_1.docx")

	docx2 := r.Editable()
	docx2.Replace("old_2_1", "new_2_1", -1)
	docx2.Replace("old_2_2", "new_2_2", -1)
	docx2.WriteToFile("./new_result_2.docx")

	// Or write to ioWriter
	// docx2.Write(ioWriter io.Writer)

	docx3 := r.Editable()
	//Currently only swaps apples for apples i.e. png to png, and not png to jpeg etc.
	docx3.ReplaceImage("word/media/image1.png", "./new.png")

	// replace the last image
	imageIndex := docx3.ImagesLen()
	docx3.ReplaceImage("word/media/image"+strconv.Itoa(imageIndex)+".png", "./new.png")
	docx3.WriteToFile("./new_result_3.docx")

	r.Close()
}

docx's People

Contributors

adsian avatar akovalyov avatar annaddicks avatar jamescullum avatar joschahenningsen avatar khnom5000 avatar mzimmerman avatar nguyenthenguyen avatar securityguy avatar stewartwebb avatar trentstollery avatar wangming1993 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

docx's Issues

New line

Is there a way to create/add newline? I tried several ways to add new line to a text and replace to the docx file. But every whitespace becomes a space.

No license

This is a great simple library. Thanks @nguyenthenguyen

There is no license for this project. Would you like to add one?

替换字符串时,传入键覆盖包括问题,比如 cover 与 str_cover ...

When replacing strings, passing keys overrides the problem, for example cover and str_cover. The string to be searched is cover to replace aaabbbcccc , and there is also another string to be replaced, str_cover to be replaced with qqqwwweeee. Although the replacement can generate a docx file, opening it with Office fails, and the file content is problematic. Please consider this issue accordingly.

panic: zip: not a valid zip file

package main

import (
"fmt"

"github.com/nguyenthenguyen/docx"

)

func main() {

// Read from docx file
r, err := docx.ReadDocxFile("./a.docx")
// Or read from memory
// r, err := docx.ReadDocxFromMemory(data io.ReaderAt, size int64)

// Or read from a filesystem object:
// r, err := docx.ReadDocxFromFS(file string, fs fs.FS)

if err != nil {
	fmt.Println("----")
	panic(err)
	fmt.Println("====")
}
docx1 := r.Editable()
// Replace like https://golang.org/pkg/strings/#Replace
docx1.Replace("OS_CPU", "OK", -1)

docx1.WriteToFile("./a.docx")

r.Close()

}

PS D:\Go\src\testdoc> go run .\testdoc.go

panic: zip: not a valid zip file

goroutine 1 [running]:
main.main()
D:/Go/src/testdoc/testdoc.go:21 +0x1a5
exit status 2

Fattened replaced images

If you try to replace an image, it is placed in the same place, with the same width and the same height as the original in the document. The problem is when you try to replace an image with different width or height, it looks flattened.

read word document wrong

I find the content is not right, the word has been spitted.
二、租赁房屋结构:house_body 面积:house_size平方米

the content :
二、租赁房屋结构:</w:t></w:r><w:proofErr w:type="spellStart"/><w:r w:rsidR="00B9317A"><w:rPr><w:rFonts w:ascii="方正仿宋_GBK" w:eastAsia="方正仿宋_GBK" w:hint="eastAsia"/><w:sz w:val="24"/></w:rPr><w:t>ho</w:t></w:r><w:r w:rsidR="00B9317A"><w:rPr><w:rFonts w:ascii="方正仿宋_GBK" w:eastAsia="方正仿宋_GBK"/><w:sz w:val="24"/></w:rPr><w:t>use_body</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidR="00C32FC4"><w:rPr><w:rFonts w:ascii="方正仿宋_GBK" w:eastAsia="方正仿宋_GBK" w:hint="eastAsia"/><w:sz w:val="24"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r w:rsidR="00723995"><w:rPr><w:rFonts w:ascii="方正仿宋_GBK" w:eastAsia="方正仿宋_GBK" w:hint="eastAsia"/><w:sz w:val="24"/></w:rPr><w:t>面积:</w:t></w:r><w:r w:rsidR="00B9317A"><w:rPr><w:rFonts w:ascii="方正仿宋_GBK" w:eastAsia="方正仿宋_GBK" w:hint="eastAsia"/><w:sz w:val="24"/></w:rPr><w:t>ho</w:t></w:r><w:r w:rsidR="00B9317A"><w:rPr><w:rFonts w:ascii="方正仿宋_GBK" w:eastAsia="方正仿宋_GBK"/><w:sz w:val="24"/></w:rPr><w:t>use_body</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:rPr><w:rFonts w:ascii="方正仿宋_GBK" w:eastAsia="方正仿宋_GBK" w:hint="eastAsia"/><w:sz w:val="24"/></w:rPr><w:t>平方米</w:t></w:r></w:p><w:p w:rsidR="00AE4936" w:rsidRDefault="00AE4936" w:rsidP="00AE4936"><w:pPr><w:spacing w:line="460" w:lineRule="exact"/><w:ind w:firstLine="630"/><w:rPr><w:rFonts w:ascii="方正仿宋_GBK" w:eastAsia="方正仿宋_GBK"/><w:sz w:val="24"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:ascii="方正仿宋_GBK" w:eastAsia="方正仿宋_GBK" w:hint="eastAsia"/><w:sz w:val="24"/></w:rPr><w:t>

what is wrong with it ?

Feature request: add replace image function

Hi, thank you for your awesome work!

It would be nice to have a ReplaceImage() function, the function could be like the following:

func ReplaceImage(oldFilePath, newFilePath)

In which the oldFilePath is the image file name inside word/media, and the newFilePath is the file path we want to replace into.

And so far I've figured out there will be 3 things to do to replace an image:

  1. Compress the image file and copy it into word/media/... and overwrite the old file.
  2. Modify the <Relationship Id="..." Type="..." Target="media/image1.png"/> line according to the new image file format.
  3. Register image file type to [Content_Types].xml if it doesn't contain the corresponding file type.

Export to PDF?

Hi Ijust wanted to use your library, but noticed, after I replaced the variables/placeholders I can not export it to PDF.
If this package does not provide this, is there another package, that is able to do so?

If so, is it possible to to so stateless? As I dont ever want to store the file, just to read and delete it again.
So would be cool if you would add the the option to export the DOCX as PDF.

Thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.