doc to docx golang

WBOY
Release: 2023-05-10 10:23:36
Original
1300 people have browsed it

Today, I want to share with you how to use Golang to convert doc files into docx files.

With the update of the Microsoft Office suite, the doc file format has been gradually phased out, and now the docx file format has become a more common document format. If you need to process documents in your own application, then converting doc to docx is necessary.

Golang is a powerful programming language that performs very well in handling tasks such as document conversion. Below, I will introduce how to use Golang to complete the task of converting doc to docx.

First, we need to use the third-party library "github.com/unidoc/unioffice" to complete this task. The "unioffice" library depends on another library "github.com/antchfx/xmlquery", so we need to introduce these two libraries into the project:

go get github.com/unidoc/unioffice go get github.com/antchfx/xmlquery
Copy after login

Next, we need to read from the doc file Take the text content and convert it into docx formatted text. The following is a simple sample code:

package main import ( "fmt" "github.com/antchfx/xmlquery" "github.com/unidoc/unioffice/document" "io" "os" "path/filepath" ) func convertDocx(filePath string) error { f, err := os.Open(filePath) if err != nil { return err } defer f.Close() r, err := document.Open(f) if err != nil { return err } docxFilePath := filepath.Join(filepath.Dir(filePath), fmt.Sprintf("%s.docx", filepath.Base(filePath))) f2, err := os.Create(docxFilePath) if err != nil { return err } defer f2.Close() w, err := document.Create(f2, document.WithTemplate(r)) if err != nil { return err } for _, para := range r.Paragraphs() { for _, run := range para.Runs() { if run.IsLineBreak() { w.AddLineBreak() } else if run.IsTab() { w.AddTab() } else if run.IsPicture() { io.Copy(w, r.GetPictureData(run.Picture())) } else { w.WriteString(run.Text()) } } w.AddParagraph() } w.Close() r.Close() return nil } func main() { err := convertDocx("test.doc") if err != nil { fmt.Println(err) return } fmt.Println("Conversion complete!") }
Copy after login

In the above code, we first open the doc file and read it into a document object. We then create a new docx file and pass it in as the "template" parameter of the document object. Next, we traverse each paragraph and each running instance in the doc file, convert it into the corresponding docx format and write it to the file. Finally, we close the file stream object and return nil to indicate that processing is complete.

With the above code example, we can use Golang to convert doc files into docx files. It is worth noting that in actual use, we also need to consider the handling of exceptions and the robustness of the program.

To sum up, this article introduces how to use Golang to convert doc files into docx files. I hope this article can help you and provide you with some help when dealing with document conversion tasks.

The above is the detailed content of doc to docx golang. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!