[Python]パワーポイントのテンプレート作成

パワーポイント TIPS
パワーポイント
TIPS言語

利用方法

  1. ライブラリのインストール(初回のみ): お使いのPCのターミナル(またはコマンドプロンプト)で、以下のコマンドを実行して必要なライブラリをインストールしてください。
    pip install python-pptx
  2. 設定セクションをカスタマイズ:
    • スクリプト冒頭の # --- テンプレート設定 --- セクションを開きます。
    • "output_filename" に、作成したいファイル名を入力します。
    • "company_name" や "copyright_text" を自社の情報に変更します。
    • "main_font" を、PCにインストールされているお好みの日本語フォント名(例: 'メイリオ', 'MS Pゴシック'など)に変更します。
    • 各 ...font_size の値を変更して、全体の文字サイズを調整します。
    • ...color の値を変更して、キーカラーを調整します。
  3. スクリプトの実行: 設定を編集・保存した後、ターミナルで以下のコマンドを実行します。
    python create_ppt_template.py
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN
from pptx.enum.shapes import MSO_SHAPE_TYPE
from datetime import datetime

# =============================================================================
# --- テンプレート設定 ---
# このセクションの値を変更するだけで、テンプレート全体のデザインをカスタマイズできます。
# =============================================================================

SETTINGS = {
    # --- ファイルと基本情報 ---
    \"output_filename\": \"Corporate_Template.pptx\",
    \"company_name\": \"株式会社 YOUR COMPANY\",
    \"copyright_text\": f\"© {datetime.now().year} YOUR COMPANY NAME\",

    # --- 基本フォント ---
    \"main_font\": \"Yu Gothic\",  # PCにインストールされているフォント名(例: \'メイリオ\', \'MS Pゴシック\')

    # --- ヘッダー・フッターの文言とフォントサイズ ---
    # ヘッダー
    \"header_left_text\": \"重要書類\",
    \"header_left_font_size\": 10,
    # フッター
    \"footer_left_company_name_font_size\": 11,
    \"footer_center_copyright_font_size\": 10,
    \"footer_right_page_number_font_size\": 11,

    # --- 各スライド要素のフォントサイズ ---
    \"main_title_font_size\": 48,      # 表紙のメインタイトル
    \"subtitle_font_size\": 28,        # 表紙のサブタイトル
    \"page_title_font_size\": 36,      # 各ページのタイトル
    \"page_content_font_size\": 18,    # 各ページのコンテンツ(本文)

    # --- 色設定 (RGB) ---
    \"header_footer_color\": RGBColor(128, 128, 128), # ヘッダー・フッターの文字色 (灰色)
    \"title_color\": RGBColor(0, 51, 102),           # 各ページタイトルの色 (濃い青)
    \"body_text_color\": RGBColor(50, 50, 50),       # 本文の文字色 (濃い灰色)
    \"main_title_color\": RGBColor(0, 0, 0),         # メインタイトルの色 (黒)
}

# =============================================================================
# --- プレゼンテーション生成ロジック ---
# 基本的にこのセクションは変更する必要はありません。
# =============================================================================

def apply_font_style(run, font_name, size_pt, color_rgb=None, bold=False):
    \"\"\"テキスト(run)にフォントスタイルを適用するヘルper関数\"\"\"
    font = run.font
    font.name = font_name
    font.size = Pt(size_pt)
    font.bold = bold
    if color_rgb:
        font.color.rgb = color_rgb

def create_template():
    \"\"\"設定に基づいてPowerPointテンプレートを生成します。\"\"\"
    prs = Presentation()
    prs.slide_width = Inches(16)
    prs.slide_height = Inches(9)

    # --- スライドマスターの編集 (全スライド共通のヘッダー・フッターを設定) ---
    slide_master = prs.slide_masters[0]

    # Iterate through shapes on the master and modify existing text boxes
    for shape in slide_master.shapes:
        if shape.has_text_frame:
            tf = shape.text_frame
            # Simple heuristic: check vertical position to guess header/footer area
            # This is not foolproof and depends heavily on the default template
            if shape.top < Inches(1): # Likely header area
                # Attempt to add header text
                tf.clear()
                p = tf.paragraphs[0]
                run = p.add_run()
                run.text = SETTINGS[\"header_left_text\"]
                apply_font_style(run, SETTINGS[\"main_font\"], SETTINGS[\"header_left_font_size\"], SETTINGS[\"header_footer_color\"])
                p.alignment = PP_ALIGN.LEFT
            elif shape.top > Inches(8): # Likely footer area
                 tf.clear()
                 p = tf.paragraphs[0]

                 # Attempt to add Company Name (left)
                 run = p.add_run()
                 run.text = SETTINGS[\"company_name\"]
                 apply_font_style(run, SETTINGS[\"main_font\"], SETTINGS[\"footer_left_company_name_font_size\"], SETTINGS[\"header_footer_color\"])
                 p.alignment = PP_ALIGN.LEFT

                 # Attempt to add Copyright (center)
                 # This might overwrite the previous text or append depending on the shape structure
                 # A better approach is to find specific placeholders if they exist
                 # For demonstration, let\'s just add it as a new paragraph if possible
                 if len(tf.paragraphs) > 0:
                     p = tf.add_paragraph()
                 run = p.add_run()
                 run.text = SETTINGS[\"copyright_text\"]
                 apply_font_style(run, SETTINGS[\"main_font\"], SETTINGS[\"footer_center_copyright_font_size\"], SETTINGS[\"header_footer_color\"])
                 p.alignment = PP_ALIGN.CENTER

                 # Attempt to add Page Number (right)
                 if len(tf.paragraphs) > 0:
                     p = tf.add_paragraph()
                 run = p.add_run()
                 run.text = \"{}\" # Placeholder for slide number
                 apply_font_style(run, SETTINGS[\"main_font\"], SETTINGS[\"footer_right_page_number_font_size\"], SETTINGS[\"header_footer_color\"])
                 p.alignment = PP_ALIGN.RIGHT

    # --- サンプルスライドの生成 ---
    # 1. 表紙スライド
    title_slide_layout = prs.slide_layouts[0]
    slide = prs.slides.add_slide(title_slide_layout)
    slide.display_master_shapes = False # 表紙ではヘッダー/フッターを非表示

    title = slide.shapes.title
    subtitle = slide.placeholders[1]
    title.text = \"ここにメインタイトルを入力\"
    subtitle.text = \"ここにサブタイトルを入力\n日付や部署名など\"

    # フォント設定
    apply_font_style(title.text_frame.paragraphs[0].runs[0], SETTINGS[\"main_font\"], SETTINGS[\"main_title_font_size\"], SETTINGS[\"main_title_color\"], bold=True)
    apply_font_style(subtitle.text_frame.paragraphs[0].runs[0], SETTINGS[\"main_font\"], SETTINGS[\"subtitle_font_size\"], SETTINGS[\"body_text_color\"])
    # Check if there is a second paragraph before applying style
    if len(subtitle.text_frame.paragraphs) > 1 and subtitle.text_frame.paragraphs[1].runs:
        apply_font_style(subtitle.text_frame.paragraphs[1].runs[0], SETTINGS[\"main_font\"], SETTINGS[\"subtitle_font_size\"], SETTINGS[\"body_text_color\"])

    # 2. コンテンツスライド
    content_slide_layout = prs.slide_layouts[1]
    slide = prs.slides.add_slide(content_slide_layout)

    title = slide.shapes.title
    title.text = \"各ページのタイトル\"
    apply_font_style(title.text_frame.paragraphs[0].runs[0], SETTINGS[\"main_font\"], SETTINGS[\"page_title_font_size\"], SETTINGS[\"title_color\"], bold=True)

    content = slide.placeholders[1]
    tf = content.text_frame
    tf.clear() # 既存のテキストをクリア
    p = tf.add_paragraph() # Add a new paragraph after clearing
    p.text = \"ここに本文を入力します。\"
    apply_font_style(p.runs[0], SETTINGS[\"main_font\"], SETTINGS[\"page_content_font_size\"], SETTINGS[\"body_text_color\"])

    # 箇条書きレベルのスタイル設定
    p = tf.add_paragraph()
    p.text = \"箇条書きレベル1\"
    p.level = 1
    apply_font_style(p.runs[0], SETTINGS[\"main_font\"], SETTINGS[\"page_content_font_size\"], SETTINGS[\"body_text_color\"])

    p = tf.add_paragraph()
    p.text = \"箇条書きレベル2\"
    p.level = 2
    apply_font_style(p.runs[0], SETTINGS[\"main_font\"], SETTINGS[\"page_content_font_size\"], SETTINGS[\"body_text_color\"])

    # --- ファイルに保存 ---
    prs.save(SETTINGS[\"output_filename\"])
    print(f\"\'{SETTINGS[\'output_filename\']}\' としてテンプレートを保存しました。\")

if __name__ == \'__main__\':
    create_template()
[zsh]シェル変更時の手順と注意点
ログインシェルを安全に変更・移行するための手順と注意点を解説。chshコマンドによる恒久変更やOS別の注意点に加え、環境変数や自作関数のポータビリティを高める設定ファイルの移行ノウハウまで、実務に役立つ知見を凝縮しています。
Q よくある質問 (FAQ)
Q. python-pptxで日本語フォントを指定する際の注意点は何ですか?
A.
スクリプト内のフォント設定(例: 'メイリオ' や 'MS Pゴシック')に、実行するPCに実際にインストールされている正確なフォント名を入力する必要があります。
Q. テンプレートの配色や文字サイズをカスタマイズするにはどうすればよいですか?
A.
スクリプト冒頭の「テンプレート設定」セクションにある、各font_sizeやcolorの値を変更することで、全体のデザインを自由に調整できます。

コメント

タイトルとURLをコピーしました