Extracting images embedded in Excel Files

15 views (last 30 days)
Jaco on 27 Aug 2014
Edited: DGM on 6 Feb 2023
Hi Everybody, I would like to know if there is a method for extracting images embedded in an Excel (*.xlsx) file?

Answers (2)

Tushar Behera
Tushar Behera on 6 Feb 2023
Hi jaco,
I believe you want to know how to extract images from excel files.
You can use "ActiveX" function to acheive this. For example you can refer to this Matlab answer,
hope this resolves your query.
  1 Comment
Jaco Labuschagne
Jaco Labuschagne on 6 Feb 2023
Hi Tushar, Gasp. 9 years later... (You know how long I've been holding a pee waiting for that answer... 🤣😂🤣). Thanks for the answer. I just wish I could remember what I was trying to do with the question. I will dig through and see. :D

Sign in to comment.

DGM on 6 Feb 2023
Edited: DGM on 6 Feb 2023
If all you need is to extract images, you can do that directly. The attached xlsx file contains two embedded PNGs and a JPG. One of the PNG files has transparent content, though note that I'm discarding it.
For what it's worth, the troubles with automagically handling the variable presence of alpha content are the same regardless of whether the images were embedded in a document or came straight from the disk.
fname = 'Book1.xlsx';
% unzip the file
tempdir = 'docdump';
% get the location of the embedded media
dirinfo = dir(fullfile(tempdir,'xl/media/*.*'));
nfiles = numel(dirinfo);
% try to read everything
allimages = {};
for k = 1:nfiles
thisfname = fullfile(dirinfo(k).folder,dirinfo(k).name);
[thispict map alpha] = imread(thisfname);
fprintf('%s is not a readable image file\n',thisfname)
if ~isempty(map)
% if the image is indexed color, convert to RGB
thispict = ind2rgb(thispict,map);
if ~isempty(alpha)
% if the image has alpha content, you'll have to figure out how you want to handle it
% you could attach it (IA/RGBA output)
% note that this won't directly work with montage/imshow/imwrite
% as nothing in MATLAB/IPT knows what to do with attached alpha
% could be worked around by conditionally splitting alpha as needed
%thispict = joinalpha(thispict,alpha); % uses MIMT (FEX)
% ... or you could composite it with some matting (collapse to I/RGB output)
% this makes it so that imshow/montage will handle the image
% but you're losing the actual alpha content
%thispict = alphasafe(joinalpha(thispict,alpha)); % uses MIMT (FEX)
% or you could do something else to manage a cumbersome
% unattached alpha (I+A/RGB+A) workflow, as is canonical with base tools
% perhaps build a second cell array containing alpha pages?
% would then require conditional handling every time you call
% imshow/imwrite/etc
allimages = [allimages {thispict}]; %#ok<AGROW>
/users/mss.system.QUqTMs/docdump/xl/media/. is not a readable image file /users/mss.system.QUqTMs/docdump/xl/media/.. is not a readable image file
% remove the temp directory
% show the images that we found
% i'm setting the bgcolor so that it's clear what the extents are
montage(allimages,'backgroundcolor',[0.7 0.3 1])
Since this doesn't use actxserver, this approach doesn't require that you're using Windows.
See also:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!