How do you insert a PNG image with Matlab Report Generator when using a Word Document Template?

18 visualizaciones (últimos 30 días)
I'm unable to add images (png... etc) or figures (or even snapshots of figures) into my report made using MATLAB Report Generator and a Microsoft Word Template with 2 'holes' in it. Everything else works fine. Text is able to go into this template without trouble (hole 1) but the image labeled "myfig.png" or img1 won't append or add into the 2nd hole.
import mlreportgen.dom.* %import matlab report generator dom package
import mlreportgen.report.* %import matlab report generator report package
D = Document('FromTemplate','docx','RW_Template_Practice.dotx'); %Name the document D and use a template made in Microsoft Word with 2 "holes"
open(D); %open document D
moveToNextHole(D); %goes to the first hole
append(D,"SN001"); %Text in First hole -- shows up fine in FromTemplate.docx
moveToNextHole(D); %goes to the second hole, made just like the first, but with a different name/tag from first hole
myfig = B_Plot_PWM_CvS_Single_P(345) %runs custom function that returns a 1x1 Figure
saveas(myfig,'myfig','png') %saves my figure as a png image
imgPath = which("myfig.png") %gives full path of image file
img1 = Image(imgPath) %make it into an image object
append(D,img1) %THIS IS WHERE THINGS GO WRONG?? I want to put an image object into the 'hole' but nothing shows up in FromTemplate.docx
close(D); %Document closes normally
rptview(D); %Document opens in Microsoft Word and "myfig.png" or img1 is nowhere to be seen
  2 comentarios
Rahul Singhal
Rahul Singhal el 3 de Ag. de 2021
Hi Tanya,
Can you share the 'RW_Template_Practice.dotx' template file, which can help us investigate the issue you are facing?
-Rahul
Tanya Mittal
Tanya Mittal el 3 de Ag. de 2021
I've attached the template file I've been struggling with. I noticed that if the template file doesn't have anything except for text in it, everything works fine and the images show up. I don't understand why the template can't have images already in it though.

Iniciar sesión para comentar.

Respuesta aceptada

Rahul Singhal
Rahul Singhal el 3 de Ag. de 2021
Hi Tanya,
The issue you are facing is because of the usage of inbuilt cover page in the template file. For the purposes of backward compatibility, Word creates two versions of the hole markup for such pages. To prevent these issues, I would highly recommend you to follow this link on how to fill holes: https://www.mathworks.com/help/rptgen/ug/fill-in-the-blanks-in-a-report-form.html.
Below is a sample on how your modified script will look like:
import mlreportgen.dom.*
import mlreportgen.report.*
D = Document('FromTemplate','docx','RW_Template_Practice.dotx');
open(D);
% Fill holes in the template
while ~strcmp(D.CurrentHoleId,"#end#")
switch D.CurrentHoleId
case "SerialNumber"
% Append content to "SerialNumber" hole which is the first hole
append(D,"SN001");
case "ImbalanceData"
% Append image to "ImbalanceData" hole which is the second hole
myfig = B_Plot_PWM_CvS_Single_P(345);
saveas(myfig,'myfig','png');
imgPath = which("myfig.png");
img1 = Image(imgPath);
append(D,img1);
end
moveToNextHole(D);
end
close(D);
rptview(D);
Thanks,
Rahul

Más respuestas (0)

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by