MATLAB で パワポページを生成するテンプレート

MATLABからPowerPointのファイルを生成するには2種類のやりかたがある。

一つはReport Generatorというツールボックスを使う方法。 もう一つはCOM(Component Object Model)と呼ばれる機能を使う方法。

手軽にやるにはReport Generatorで良いと思うが、細かく設定したい場合はCOMを使うことになる。

COMでPowerPointのファイルを生成するには、PowerPoint へ画像を貼り付けるにはどのようにすればよいですか? – MATLAB Answers – MATLAB Centralの例が参考になる。

ここでは自分用にパワポページを生成するテンプレートを貼り付けておく。

このなかでドハマりしたのは、画像に影をつける部分。

PowerPointはPicture Styleという便利が機能があって、画像を選択すると「図の形式」というタブメニューが現れ、「図のスタイル」からちょっとした装飾を施すことができる。これをVBAから使おうと調べてみると、どうやら直接「図のスタイル」を指定する方法はないらしい (かなり古いがこんなものがあった: Add a Picture Style to a Selected picture in VBA | MrExcel Message Board)。

Image1.Shadow のあたりを適当に設定すれば”Drop Shadow Rectangle”っぽいものは出来る。

%% PowerPointファイルを生成する

%% PowerPointを開く
pptx = actxserver('PowerPoint.Application');
% PowerPoint
pptx.Visible = 1; 

%% プレゼンテーションを追加
pptx.Presentation.invoke;
Presentation = pptx.Presentation.Add;
%Presentation.ApplyTheme("<full-path>toTemplate.potx")
%% カスタムレイアウトの指定
blankSlide = Presentation.SlideMaster.CustomLayouts.Item(6); % title only

%% 
slide = Presentation.Slides.AddSlide(1,blankSlide);

% スライド・タイトル
slide.Shapes.Title.TextFrame.TextRange.Text = "タイトル";

% text box
textbox = slide.Shapes.AddTextbox('msoTextOrientationHorizontal', 30, 100, 300, 20);
textbox.TextFrame.TextRange.Text = "文字列";
textbox.TextFrame.TextRange.Font.Size = 20;
% pngを挿入する
picPath = pwd + "\" + "sample_image.jpg";
Image1 = slide.Shapes.AddPicture(picPath, 'msoFalse', 'msoTrue', 30, 200);
Image1.Width = 400; % pt
%Image1.Left = 30; % pt
%Image1.Top  = 130; % pt
Image1.Shadow.Type = 21;
Image1.Shadow.Blur = 23;
Image1.Shadow.OffsetX = 7.8;
Image1.Shadow.OffsetY = 7.8;
Image1.Shadow.Size = 100;
Image1.Shadow.ForeColor.RGB = 3355443;

Image2 = slide.Shapes.AddPicture(picPath, 'msoFalse', 'msoTrue', 500, 200);
Image2.Width = 400; % pt
%Image1.Left = 30; % pt
%Image1.Top  = 130; % pt
Image2.Shadow.Type = 21;
Image2.Shadow.Blur = 23;
Image2.Shadow.OffsetX = 7.8;
Image2.Shadow.OffsetY = 7.8;
Image2.Shadow.Size = 100;
Image2.Shadow.ForeColor.RGB = 3355443;

%% プレゼンテーションを保存
Presentation.SaveAs(pwd + "\" + "MyPPTx.pptx");

%% PowerPointを閉じる
pptx.Quit;
pptx.delete;
出来あがりは下のようになる:

Adsense広告