0
1

フォルダ内のwavファイルを一音声一スライドで順に連続再生するようなPowerPointドキュメントを生成するRubyコード

Last updated at Posted at 2024-05-28

PowerPointは普通はVBでスクリプティングすると思いますが、COMコンポーネントを通じて使えば他の言語からも呼び出すことができます。
こちらVBのドキュメントですが、こちらのオブジェクトにRubyからでもアクセス出来るのでやってみました。
https://learn.microsoft.com/ja-jp/office/vba/api/overview/powerpoint
PowerPoint内でマクロとして使うものはVBでないといけないでしょうが、ひな形を出力するツールとしてなら言語を選ばずなんでも行けそうです。

実行したフォルダの直下のvoicesフォルダからwavファイルを読み込み、ファイル名順で一ファイル一スライドで配置し、再生が終われば次のスライドへ行く設定をしたドキュメントを作成し、実行したフォルダにout.pptxの名前で保存します。

require 'win32ole'
require "wavefile"

# PowerPointアプリケーションを起動
powerpoint = WIN32OLE.new('PowerPoint.Application')
# プレゼンテーションを新規作成
presentation = powerpoint.Presentations.Add
# プレゼンテーションのサイズを設定
slideWidth = 1920.0 / 96.0 * 72.0
slideHeight = 1080.0 / 96.0 * 72.0
presentation.PageSetup.SlideWidth = slideWidth
presentation.PageSetup.SlideHeight = slideHeight
# wavファイルのディレクトリを指定
wav_dir = "#{Dir.pwd}/voices"
voices = Dir.glob("#{wav_dir}/*.wav").sort
# wavファイルを順番に処理
voices.each_with_index do |wav_file, i|
  pp wav_file
  # 新しいスライドを追加
  slide = presentation.Slides.Add(presentation.Slides.Count + 1, 12)  # 12 = ppLayoutBlank
  # wavファイルを埋め込み
  shape = slide.Shapes.AddMediaObject2(wav_file)
  shape.AnimationSettings.PlaySettings.PlayOnEntry = true
  shape.AnimationSettings.PlaySettings.HideWhileNotPlaying = true
  info = WaveFile::Reader.new(wav_file)
  duration = info.total_duration
  second = duration.seconds + duration.minutes * 60.0 + duration.milliseconds / 1000.0
  slide.SlideShowTransition.AdvanceOnTime = true
  slide.SlideShowTransition.AdvanceTime = second
  # ノートにwaveファイル名を追加
  slide.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.Text = wav_file
end
# プレゼンテーションを保存
save_path = "#{Dir.pwd}/out.pptx"
if File.exist?(save_path)
  puts '既に出力ファイルが存在します。'
else
  presentation.SaveAs(save_path)
  powerpoint.ActivePresentation.Close
  powerpoint.Quit
end

スライドには音声以外なにも貼られませんが、ノートにwavファイル名が入りますのでそれを参考に各スライドを作り込んで行くことができます。これで完成品の動画を作ることもできますし、Vコンテのように使うのも良さそうです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1