Clear Filters
Clear Filters

is it possible to select the screenshots from word document and crop them and then resize them to full page?

1 view (last 30 days)
I am doing bit of learnign through videos on youtube, and taking screenshots of the slide so later on I can review and put comment on it. Is there a way in matlab where i can access the word document select all the screenshots in it and crop them to the size shown in the code below and then resize them A4 page? I found the methodology to do it for one image or can do it if the images are in folder but unsure about how to do it if the screenshots are in word document. file is attached
Code tried so far: (it is wrong because it is a code i used with the folder path and can not work with word file)
images = ['C:\Users\40085772\Desktop\PowerBI_PL300.docx'] ;
N = length(images) ;
I = imread(images(1).name) ;
[x, rect] = imcrop(I) ;
for i = 1:N
I = imread(images(i).name) ;
I2 = imcrop(I,[98.510000000000000,1.875100000000000e+02,1.285980000000000e+03,7.239800000000000e+02]);
Dot indexing is not supported for variables of this type.

Accepted Answer

DGM on 3 Jan 2023
Edited: DGM on 3 Jan 2023
Here. I'm sure there are better ways, but it seems to be simple enough that I can get away with directly editing the file.
% document is zipped so that the forum editor allows it; unzip it
% document itself is a zip archive; unzip it to a temp directory
tempdir = 'docxdump';
% get the location of the embedded images
dirinfo = dir(fullfile(tempdir,'word/media/*.png'));
nfiles = numel(dirinfo);
% crop them and put them back
for k = 1:nfiles
thisfname = fullfile(dirinfo(k).folder,dirinfo(k).name);
thispict = imread(thisfname);
thispict = imcrop(thispict,[108 194 1263 710]);
% take the contents of the temp directory and re-zip it
outfilename = 'modified';
dirinfo = dir(fullfile(tempdir,'*'));
dirinfo = dirinfo(~ismember({},{'.','..'}));
zip([outfilename '.docx'],{},tempdir)
movefile([outfilename ''],[outfilename '.docx'])
% remove the temp directory
At least that works in this simple case. The image objects are simply scaled to fit the same existing rectangular region in the document. If the cropped images had an aspect ratio that differed from the uncropped images, there would be problems.

More Answers (1)

Praharshita on 3 Jan 2023
Hi muhammad,
I understand that you are facing an issue while reading the image.
Try using imread to read the image.
I hope this helps.
  1 Comment
DGM on 3 Jan 2023
Edited: DGM on 3 Jan 2023
Can you point to an example where imread() accepts .docx files?
I thought this would be a job for actxserver(), but I don't run windows, and neither does the forum editor.

Sign in to comment.


Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!