go-docx
go-docx copied to clipboard
I hope that this library will be able to replace images in the future.
I hope that this library will be able to replace images in the future.
This library mainly deals with the WordprocessingML spec. Images are part of the DrawingML spec, hence I would need to work myself into that.
Nevertheless, how would you indicate inside the document that the image needs to be replaced? Using text-based placeholders seems tricky. From quickly scanning over the spec, I also don't see a way to add custom metadata to images: http://officeopenxml.com/drwPic.php
Maybe you've got an idea 😃
In order to draw the preset docx document, I have tried various libraries of golang, unioffice[carmel/gooxml] cannot replace the information in the table, but can replace the paragraph text, and finally I chose the html template to convert docx through pandoc
Hi @lukasjarosch ,
I have an idea borrowed from github.com/nguyenthenguyen/docx lib. It has a method called ReplaceImage
, which receives two filename:
- one is the original image path inside the docx, e.g. "word/media/image1.png",
- the other is the path of the image to be replaced
The lib basically reads the replacement image, and write to the original image path.
In go-docx, I find a method called SetFile, which accepts a path inside docx, and bytes to be replaced. I think this method can be used to replace image, as long as go-docx reads media files (Regex: word/media/*) in parseArchive
. With this be implemented, one can:
- Place some placeholder images in advance
- Get the placeholder image path inside docx, e.g. "word/media/image1.png"
- Use SetFile("word/media/image1.png", imageBytes) to replace it
Do you think this idea acceptable? If it is, I'm willing to send a PR, it should be very small.
Hey @TeCHiScy,
this is a great idea 🚀. For some reason I kept thinking about "inserting new images" instead of "replace the existing file, dummy" 😆
I love the idea, please feel absolutely free to send a PR over 👍🏼