mirror of
https://github.com/jmcnamara/libxlsxwriter.git
synced 2026-05-15 14:15:54 -06:00
[GH-ISSUE #417] How do I insert a picture as embedded in a cell ? #328
Labels
No labels
awaiting user feedback
bug
cmake
cmake
docs
feature request
in progress
long term
medium term
medium term
pull-request
question
question
ready to close
short term
under investigation
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: github-starred/libxlsxwriter#328
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @zhjiarong on GitHub (Nov 7, 2023).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/417
I want to insert a picture as embedded in a cell. How do I do that with libxlsxwriter? thank you

@jmcnamara commented on GitHub (Nov 7, 2023):
When you perform this action in Excel it scales the image to fit the cell.
It is also possible with
libxslxwriterto scale images (usingworksheet_insert_image_opt(). However, there aren't any helper functions to scale it to a cell/pixel size. I had planned to do this at some stage but I didn't get around to it.You can do it yourself by using some library to get the X and Y pixel size of the the image and also the X and Y DPI. There are internal functions to do this in
libxslxwriterbut they aren't public.After that you need to scale the image (using the dimensions and DPI) to the pixel size of the cell. There is some code for that in the Rust version of the library that I wrote. See here.
@zhjiarong commented on GitHub (Nov 10, 2023):
ok, thank you, I hope to add this feature in the future.
@sunilsabraham commented on GitHub (Nov 30, 2023):
@jmcnamara : The author is asking how to embed picture in cell. In latest excel there are 2 options to insert picture.
The first option is to embed picture in cell which automatically scale the picture by excel. The second once is the current one implemented by libxlswriter. Is it possible to add the first option as well. Hope you can implement it asap. Thanks.
@jmcnamara commented on GitHub (Dec 1, 2023):
@sunilsabraham Could you attach an Excel example with an image embedded in a cell and a similar example with an image just inserted, and not embedded, for reference.
@sunilsabraham commented on GitHub (Dec 1, 2023):
@jmcnamara Please see the screen shot below. Just open excel and try to insert a picture and you will get 2 options. first one is to embed picture to a cell and excel will automatically scale the image. second one is to place image over a cell and that is the current implementation in libxlswriter. Old excel had only place over a cell may be upto 2016 version. If you can implement the embed in cell that would be great. so we dont need to scale the image and it automatically does according to row height etc. I am attaching an example file as well.
Book1.xlsx
@jmcnamara commented on GitHub (Dec 1, 2023):
@sunilsabraham Thanks for pointing that out. That is a useful feature. I'll try add it.
@sunilsabraham commented on GitHub (Dec 3, 2023):
Thanks.
@rickabitdigital commented on GitHub (Dec 12, 2023):
@jmcnamara I'd love the Place in cell feature too! Images placed in cells will scale according to column width/row height. As we can define the initial column width and row height, to accommodate our inserted images, cells shoudn't be hidden on file open. I think this produces a more natural and robust layout behaviour - e.g. when objects float over cells users can struggle with the overlap.
@rickabitdigital commented on GitHub (Jan 24, 2024):
Hi @jmcnamara
Have you looked into implementing this feature yet?
I've been reading the openxml API reference docs (my first time), but the 'place in cell' xml format is not immediately obvious. So I have begun using an OOXML editor/viewer to see the difference between images placed in cells using typical drawing references vs an image embedded in a cell.
I'll let you know if I get anywhere.
If there's any way you can think of me helping advance this feature please could you advise?
thank you.
@jmcnamara commented on GitHub (Jan 24, 2024):
Yes. I started working on it in December but then got sucked into some work on
rust_xlsxwriter. I made some progress on the Metadata and RichValue files but there is still quite a bit to do. I'll start working on it again next week after the 0.62.0 release ofrust_xlsxwriterbut I can't give a timeline.@rickabitdigital commented on GitHub (Jan 24, 2024):
Hi John,
That's great to hear and thank you very kindly for the update. Much appreciated.
I have just reached the point where the basic OOXML viewer I found wasn't showing me the RichValue relationship for an image placed in cell for me to compare with the XML of the existing drawing relationships for image objects.
I suppose I may need to give in and use the obvious OOXML explorer for VSCode.
I'll keep going with my learning and appreciate any time you find to work on this.
@slw287r commented on GitHub (Jan 29, 2024):
Snippets to get image dimensions and DPIs for in-cell embedding.
https://github.com/slw287r/imgt/blob/main/imgt.c
@jmcnamara commented on GitHub (Jan 29, 2024):
Note, there is already code like that in the library: https://github.com/jmcnamara/libxlsxwriter/blob/main/src/worksheet.c#L3885
It isn't public, by default, and I could make it public but the feature request is for a newer type of embedded image feature in Excel.
@jmcnamara commented on GitHub (Feb 19, 2024):
Just a note that I've made some progress on this. I've implemented this feature in the Python and Perl versions of the library (
XlsxWriterandExcel::Writer::XLSX). Next up I will implement it in the Rust versionrust_xlsxwriterand then I will move on to the C versionlibxlsxwriter.https://xlsxwriter.readthedocs.io/worksheet.html#embed_image
@rickabitdigital commented on GitHub (Mar 19, 2024):
Hi John,
I'm creating sheets and images in memory - in your C Library.
Will this feature be an option for the worksheet_insert_image_buffer_opt function?
@jmcnamara commented on GitHub (Apr 4, 2024):
Yes. Probably.
@rickabitdigital commented on GitHub (Jul 24, 2024):
Hey @jmcnamara, Any idea when you may revisit this feature?
kind regards
Rick
@jmcnamara commented on GitHub (Jul 28, 2024):
I'll try tackle it this week.
@jmcnamara commented on GitHub (Jul 29, 2024):
I've pushed an initial functional version of this feature to main.
It works as follows (from the examples folder):
Which gives this output:
There is still some work to do:
Add support for alt_text/descriptionDoneAdd support for urlsDoneAdd support for cell format behind the imageDoneAdd buffer supportDoneDocs and examplesDoneAs an aside I'd like to refactor image handling into an Image struct that can be constructed from a path or a buffer and could be configured without the
lxw_image_optionsstruct. It could also then return the calculated dimensions of the image. Something like the Rust interface: https://docs.rs/rust_xlsxwriter/latest/rust_xlsxwriter/struct.Image.html However, that will be for another day.@jmcnamara commented on GitHub (Jul 30, 2024):
I've added support for image options such as urls and alt text and also support for creating the image from a buffer:
Output:
@jmcnamara commented on GitHub (Jul 31, 2024):
Available upstream in
v1.1.8.@HEUBITLYJ commented on GitHub (Jul 1, 2025):
@jmcnamara
hello, thank you for implementing worksheet_embed_image, but since i run the example code:
`
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("embed_images2.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, "sheet1");
`
i open the embed_images2.xlsx with office 2021, the image does not display in xlsx:
is there something wrong with my operation?
looking forward to your reply
@jmcnamara commented on GitHub (Jul 1, 2025):
This feature was added in Excel 365 in 2023. Older versions don't support it and you get an "#UNKNOWN!" or " #VALUE!" error by default.
https://libxlsxwriter.github.io/worksheet_8h.html#afaf00521027cd79c6d7d93537848b283