go-docx icon indicating copy to clipboard operation
go-docx copied to clipboard

I hope that this library will be able to replace images in the future.

Open Summer-Time-2020 opened this issue 1 year ago • 4 comments

I hope that this library will be able to replace images in the future.

Summer-Time-2020 avatar Apr 03 '23 01:04 Summer-Time-2020

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 😃

lukasjarosch avatar May 16 '23 19:05 lukasjarosch

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

YaEvan avatar Jul 11 '23 07:07 YaEvan

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.

TeCHiScy avatar Apr 18 '24 17:04 TeCHiScy

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 👍🏼

lukasjarosch avatar Apr 19 '24 16:04 lukasjarosch