1
0

More than 1 year has passed since last update.

python-pptxでパワポ作成自動化1

Last updated at Posted at 2022-07-31

パワーポイントを自動的に作成するための初歩的な関数。
大まかなところは分かった。
あとはシェイプ同士を接続する線を入力する方法が分かれば自動でネットワーク図がかけそう。

from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.dml.color import RGBColor
from pptx.util import Pt

WHITE = RGBColor(255, 255, 255)
OLIVE = RGBColor(128, 128, 0)
YELLOW = RGBColor(255, 255, 0)
FUCHSIA = RGBColor(255, 0, 255)
SILVER = RGBColor(192, 192, 192)
AQUA = RGBColor(0, 255, 255)
LIME = RGBColor(0, 255, 0)
RED = RGBColor(255, 0, 0)
GRAY = RGBColor(128, 128, 128)
BLUE = RGBColor(0, 0, 255)
GREEN = RGBColor(0, 128, 0)
PURPLE = RGBColor(128, 0, 128)
BLACK = RGBColor(0, 0, 0)
NAVY = RGBColor(0, 0, 128)
TEAL = RGBColor(0, 128, 128)
MAROON = RGBColor(128, 0, 0)

DEFAULT_COLOR = BLUE
DEFAULT_SHAPE = MSO_SHAPE.RECTANGLE # https://qiita.com/fuji3195/items/5e1a2f5760e98ac7116b#%E4%BB%A5%E4%B8%8B%E4%B8%80%E8%A6%A7


def headline(sld, _title, _subtitle):
    # スライドタイトルとサブタイトルを入力する関数
    title = sld.shapes.title
    title.text = _title
    subtitle = sld.placeholders[1]
    p = subtitle.text_frame.add_paragraph()
    p.text = _subtitle


def shape(sld, _text, x, y, width, hight, shapetype=DEFAULT_SHAPE, fontsize=10, color=DEFAULT_COLOR):
    # オートシェイプを配置する関数
    rect = sld.shapes.add_shape(
        shapetype,   	      # 図形の種類を[丸角四角形]に指定
        Pt(x), Pt(y),         # 挿入位置の指定 左上の座標の指定
        Pt(width), Pt(hight))  # 挿入図形の幅と高さの指定

    # [B]図形の塗り潰しとテキストの設定
    rect.fill.solid()                                   # shapeオブジェクト➀を単色で塗り潰す
    rect.fill.fore_color.rgb = color  # RGB指定で色を指定

    pg = rect.text_frame.paragraphs[0]   # shapeオブジェクト➀のTextFrameの取得
    pg.text = _text         	         # TextFrameにテキストを設定
    pg.font.size = Pt(fontsize)          # テキストの文字サイズを10ポイントとする


if __name__ == "__main__":
    prs = Presentation()
    sld = prs.slides.add_slide(prs.slide_layouts[1])  # 空白のスライドを追加
    headline(sld, "これはタイトルです", "そしてサブタイトルです\nそして2行目です")
    shape(sld, "これは四角形ですね", 100, 300, 200, 100)
    prs.save('Blog_図形の挿入.pptx')


1
0
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
1
0