How do I add a high resolution figure to a Word template using Report Generator?
22 次查看(过去 30 天)
显示 更早的评论
I am using Report Generator to populate a Word template with text/numerical data and plots from laboratory testing. The template has Word-formatted tables with fillable holes followed by holes between tables to fill with figures. I'm currently using the approach below to sequentially fill holes. The entry below shows a text entry followed by a figure.
import mlreportgen.dom.*
D = Document('FromTemplate','docx', 'UCS_Word_Template.dotx');
open(D)
moveToNextHole(D);
append(D, app.ProjectEditField.Value);
moveToNextHole(D);
myfig=figure();
%plot code in this section working as intended. Not included here to
%simplify description.
fontsize(myfig,0.25,"centimeters")
myfig.PaperPositionMode = 'manual';
myfig.PaperUnits = 'inches';
myfig.PaperPosition = [0 0 4.75 3];
saveas(myfig,'myfig','png');
imgPath = which("myfig.png");
img1 = Image(imgPath);
append(D,img1);
This code is working as intended, but saveas uses a set resolution that is not adjustable (to my knowledge). The resulting figure has fine detail that is blurry in the printed report.
I have also tried using the exportgraphics function and setting the size as shown below:
import mlreportgen.dom.*
D = Document('FromTemplate','docx', 'UCS_Word_Template.dotx');
open(D)
moveToNextHole(D);
append(D, app.ProjectEditField.Value);
moveToNextHole(D);
t = tiledlayout(1,1, 'Padding','tight');
t.Units = 'inches';
t.OuterPosition = [0.25 0.25 4.75 3];
nexttile;
%plot code in this section working as intended. Not included here to
%simplify description.
exportgraphics(t, 'myfig.png','Resolution', 600);
imgPath = which("myfig.png");
img1 = Image(imgPath);
append(D,img1);
This code also works, but the figure filled into the word template is approximately 18x22 inches.
Is there a way to append an image into the template with both a set size and set resolution?
0 个评论
采纳的回答
Mary Abbott
2022-6-21
Hi Richard,
You can specify the height and width of an image in a report with properties of the mlreportgen.dom.Image class. For example, using your "exportgraphics" approach, you can resize the image in the following way:
imgPath = which("myfig.png");
img1 = Image(imgPath);
img1.Width = "4.75in";
img1.Height = "3in"
append(D,img1);
To get the highest quality image, I recommend using the SVG file format, as long as you are using Word 2016 or later. With your "saveas" approach, this would look like:
saveas(myfig,'myfig','svg');
imgPath = which("myfig.svg");
img1 = Image(imgPath);
% (you can also change the image width and height here instead of adjusting
% the figure's PaperPosition)
append(D,img1);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Printing and Saving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!