Skip to content

Instantly share code, notes, and snippets.

@mgng
Last active April 10, 2018 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgng/75ee5753d6c5b38adcc585f6ed7c1d60 to your computer and use it in GitHub Desktop.
Save mgng/75ee5753d6c5b38adcc585f6ed7c1d60 to your computer and use it in GitHub Desktop.
PHP の COM クラスを使って PowerPoint ファイルのスライドに文字列を挿入するサンプル
<?php
// 参考 : https://msdn.microsoft.com/ja-jp/vba/powerpoint-vba/articles/slide-object-powerpoint
// パワーポイントファイル名
$filename = __DIR__ . "/data/test.pptx";
// COM インスタンス生成
$ppt_app = new \COM("PowerPoint.Application");
// ファイル読み込み
$presentation = $ppt_app->Presentations->Open($filename);
// スライドを1枚ずつ処理。Indexが 1スタートなので注意
for($idx=1, $count=$presentation->Slides->Count; $idx <= $count; $idx++){
// スライドオブジェクト
$Slide = $presentation->Slides[$idx];
// テキストボックスを追加
// 第一引数については以下にヒントあり
// https://stackoverflow.com/questions/12231435/what-is-constant-value-of-msotextorientationhorizontal-in-vba/12237734
// msoTextOrientationMixed = 0xfffffffe (-2)
// msoTextOrientationHorizontal = 1
// msoTextOrientationUpward = 2
// msoTextOrientationDownward = 3
// msoTextOrientationVerticalFarEast = 4
// msoTextOrientationVertical = 5
// msoTextOrientationHorizontalRotatedFarEast = 6
$textShape = $Slide->Shapes->AddTextbox(1, 10,10, 200,20);
// フォントサイズ
$textShape->TextEffect->FontSize = 15;
// 文字の太さ
$textShape->TextEffect->FontBold = true;
// 文字色を赤に設定。順番が RGB ではなく GBR になっているぽい?
$textShape->TextFrame->TextRange->Font->Color->RGB = 0x0000FF;
// テキスト追加。SJISにしないと埋め込めない。
$textShape->TextFrame->TextRange->Text = mb_convert_encoding("追加テキスト:{$idx}", "SJIS-win", "UTF-8");
}
// 保存
$presentation->Save();
// 終了
$presentation->Close();
$ppt_app->Quit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment