Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.docx get corrupted after writing file #26

Open
cyberpunx opened this issue Apr 12, 2024 · 2 comments
Open

.docx get corrupted after writing file #26

cyberpunx opened this issue Apr 12, 2024 · 2 comments
Labels
bug Something isn't working help wanted Extra attention is needed question Further information is requested

Comments

@cyberpunx
Copy link

I'm not sure of what's happening here. I have a perfectly functioning .docx as input file. But after parse it (didn't mody a thing) and write it as new file.. the output file gets corrupted)

func OpenDocx(path string) *docx.Docx {
	readFile, err := os.Open(path)
	util.Panic(err)
	fileInfo, err := readFile.Stat()
	util.Panic(err)
	size := fileInfo.Size()
	doc, err := docx.Parse(readFile, size)
	util.Panic(err)
	return doc
}

func WriteDocx(doc *docx.Docx, path string) {
	f, err := os.Create(path)
	util.Panic(err)
	_, err = doc.WriteTo(f)
	util.Panic(err)
	err = f.Close()
	util.Panic(err)
}

and somwhere in my main.go

...
...
doc := docxLib.OpenDocx(inputFile)
docxLib.WriteDocx(doc, "copy.docx")
...
...

when I try to open copy.docx in Word, I cannot because of corrupted file.

@fumiama
Copy link
Owner

fumiama commented Apr 12, 2024

It is a possible problem if there are some complicated elements in your docx file because it's impossible for me to test every situation. You can delete a few elements in that file and run this code again to find out where the problem really coming from. Then you can fix it or put more detail to this issue to certain it.

@fumiama fumiama added bug Something isn't working help wanted Extra attention is needed question Further information is requested labels Apr 12, 2024
@cyberpunx
Copy link
Author

Ok, that makes sense. After what you said, I tried a simplier docx and had no issue, so definitely is something in my original docx, which is fairly complicated... it has tables, differente footers, headers, images and what not. I'll inform if I found the conflicting element. Ty for your answer!

@fumiama fumiama mentioned this issue May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants