ai.add_notes_pptx_lib のソースコード

"""
PowerPointのノートをスライドに字幕として追加するライブラリ。

このモジュールは、PowerPointプレゼンテーションファイルの各スライドに添付されているノートテキストを読み込み、
そのノートをスライド自体の下部に字幕として追加する機能を提供します。
これにより、プレゼンテーションを再生する際にノートの内容を直接確認できるようになります。

機能:
- コマンドライン引数から入力ファイルと出力ファイルを指定。
- 指定されたPowerPointファイルを開き、各スライドのノートを抽出。
- 各スライドの下部に新しいテキストボックスを作成し、抽出したノートテキストを挿入。
- 字幕のフォント、サイズ、色、背景色をカスタマイズ。
- 変更されたプレゼンテーションを新しいファイルとして保存。

使用例:
    $ python add_notes_pptx_lib.py input.pptx -o output.pptx

:doc:`add_notes_pptx_lib_usage`
"""
import argparse
import os
from pptx import Presentation
from pptx.util import Pt
from pptx.enum.text import PP_ALIGN
from pptx.dml.color import RGBColor

[ドキュメント] def parse_args(): """ コマンドライン引数を解析します。 入力PowerPointファイルと出力PowerPointファイル名をコマンドラインから取得します。 出力ファイル名が指定されなかった場合、入力ファイル名に '-note-added.pptx' を付加した名前を生成します。 :returns: tuple: (入力ファイル名, 出力ファイル名) :rtype: tuple[str, str] """ parser = argparse.ArgumentParser(description="PowerPointノートをスライドに字幕として追加するツール") parser.add_argument("infile", help="入力PowerPointファイル(.pptx)") parser.add_argument("-o", "--outfile", help="出力ファイル名(省略時は -note-added.pptx を付加)") args = parser.parse_args() if not args.outfile: base, ext = os.path.splitext(args.infile) args.outfile = f"{base}-note-added.pptx" return args.infile, args.outfile
[ドキュメント] def add_notes_as_subtitles(infile, outfile): """ PowerPointのノートを各スライドの下部に字幕として追加します。 指定されたPowerPointファイルを開き、各スライドのノートテキストを読み込みます。 読み込んだノートテキストは、スライド自体の下部に黒い背景と白い文字で表示される字幕として追加されます。 変更されたプレゼンテーションは新しいファイルとして保存されます。 :param infile: str: 入力PowerPointファイルのパス。 :param outfile: str: 出力PowerPointファイルを保存するパス。 :returns: None """ prs = Presentation(infile) # スライドサイズ取得(デフォルトは 10 x 7.5 インチ) slide_width = prs.slide_width slide_height = prs.slide_height # 字幕スタイル設定 FONT_NAME = "メイリオ" FONT_SIZE = Pt(20) FONT_COLOR = RGBColor(255, 255, 255) # 白 BACKGROUND_COLOR = RGBColor(0, 0, 0) # 黒 for slide in prs.slides: notes_slide = slide.notes_slide note_text = notes_slide.notes_text_frame.text.strip() if note_text: # テキストボックスの位置とサイズ(スライド下部に幅いっぱい) left = Pt(0) top = slide_height - Pt(60) # 下から60ptの位置 width = slide_width height = Pt(60) textbox = slide.shapes.add_textbox(left, top, width, height) textbox.fill.solid() textbox.fill.fore_color.rgb = BACKGROUND_COLOR text_frame = textbox.text_frame text_frame.word_wrap = True text_frame.text = note_text text_frame.margin_left = Pt(10) text_frame.margin_right = Pt(10) text_frame.paragraphs[0].alignment = PP_ALIGN.LEFT run = text_frame.paragraphs[0].runs[0] run.font.name = FONT_NAME run.font.size = FONT_SIZE run.font.color.rgb = FONT_COLOR prs.save(outfile) print(f"保存しました: {outfile}")
if __name__ == "__main__": infile, outfile = parse_args() add_notes_as_subtitles(infile, outfile)