LoginSignup
845

More than 5 years have passed since last update.

Pythonでパワポの説明資料(報告書)を生成する

Last updated at Posted at 2018-01-02

Pythonでパワポの報告書を生成する

この記事の対象者は以下のような人である。

  • パワーポイントで説明資料を作って偉い人に報告する人
  • 何を思ったか偉い人(経営層)がマイクロマネジメントに目覚めて毎日報告しろと言われている人
  • ディープラーニングの学習進捗を毎日報告しなければいけない人

あらまし

だいたい上記で察すると思うが、偉い人がマネジメントの病に憑りつかれ、
毎日データを報告しなければならなくなった人を想定している。
しかも、紙媒体で報告するのにパワーポイントで作成しなければならないという縛りつきである。

余談であるが、こういった状況はペーパーレスブームの際にパワーポイントで説明する文化が出来上がったものの、機材や場所の都合でプロジェクターでの説明が減り、パワーポイントの資料作りだけが残ったものと思われる。

※ なお、上記のような状況はフィクションであり、実在の企業や団体とは関係ないと信じている。

やりたいこと

その企業ないしは部署に決められたフォーマットのスライドテンプレートに、表とグラフを貼り付けて出力する。

環境

項目 内容
OS Windows 8.1 64ビット
プロセッサ Intel Core M-5Y31 CPU 0.9GHz 1.10GHz
RAM 4GB
python 3.5

1. パワーポイントを生成するPythonライブラリの導入

世の中便利なもので大体のものはすでに存在している。
python-pptx

そして先駆者が使っている。
https://qiita.com/daiki7nohe/items/11019e90cd43c82095fa
https://qiita.com/pocket8137/items/3d8fda2c47664bf9130b

以下のコマンドでインストール。
pip install python-pptx

2. サンプル:基本的なスライド

まずはGetting Startedから基本的なスライドの生成プログラムを見る。
このスタートアップガイドで図や表の挿入方法について触れられている。

プログラムの流れは
1. レイアウトの読み込み
2. スライドの追加
3. タイトルや本文の挿入
4. 保存
となっている。

test.py
from pptx import Presentation

prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]

title.text = "Hello, World!"
subtitle.text = "python-pptx was here!"

prs.save('test.pptx')

何となくこの辺をいじれば良さそうである。

helow_pptx.png
図:出力されたpptxをパワポで読んだ例

3. サンプル:グラフを含んだスライド

次にWorking with chartsからグラフを挿入する方法を見る。

chart.py
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches

# create presentation with 1 slide ------
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])

# define chart data ---------------------
chart_data = ChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))

# add chart to slide --------------------
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
slide.shapes.add_chart(
    XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
)

prs.save('chart-01.pptx')

chart.png
図:出力されたpptxをパワポで読んだ例

4. テンプレートへの落とし込み

会社であれば企業ロゴが入ったスライドが使われるものと思われる。
ここではいらすとやの画像が埋め込まれたテンプレートとなるスライドにグラフと表が張り付けられたスライドを出力する。

まずは以下のように普段使われないであろう"いらすと"が入ったスライドマスターを含むpptxファイルを作成し、作業フォルダにtemplate.pptxとして保存しておく。

ex_template.png
図 テンプレートの例

そして、上記のサンプルを組み合わせて、以下の流れのようなコードを書いた。
1. テンプレートとなるpptxファイルの読み込み
2. レイアウトの読み込み
3. スライドの追加
4. グラフ、表の追加
5. 保存

test_template.py
from pptx import Presentation
from pptx.util import Inches
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Cm #Inches
from pptx.enum.chart import XL_LEGEND_POSITION

if __name__ == '__main__':
    prs = Presentation('template.pptx')
    title_only_slide_layout = prs.slide_layouts[1]
    slide = prs.slides.add_slide(title_only_slide_layout)
    shapes = slide.shapes

    shapes.title.text = '報告書'

    name_objects = ["object1", "object2", "object3"]
    name_AIs = ["AI1", "AI2", "AI3"]
    val_AI1 = (19.2, 21.4, 16.7)
    val_AI2 = (22.3, 28.6, 15.2)
    val_AI3 = (20.4, 26.3, 14.2)
    val_AIs = [val_AI1, val_AI2, val_AI3]

    rows = 4
    cols = 4
    top    = Cm(12)
    left   = Cm(0.7) #Inches(2.0)
    width  = Cm(24) # Inches(6.0)
    height = Cm(6) # Inches(0.8)

    table = shapes.add_table(rows, cols, left, top, width, height).table

    # set column widths
    table.columns[0].width = Cm(6)# Inches(2.0)
    table.columns[1].width = Cm(6)
    table.columns[2].width = Cm(6)
    table.columns[3].width = Cm(6)

    # write column headings
    table.cell(0, 1).text = name_objects[0]
    table.cell(0, 2).text = name_objects[1]
    table.cell(0, 3).text = name_objects[2]

    # write body cells
    table.cell(1, 0).text = name_AIs[0]
    table.cell(1, 1).text = str(val_AI1[0])
    table.cell(1, 2).text = str(val_AI1[1])
    table.cell(1, 3).text = str(val_AI1[2])

    table.cell(2, 0).text = name_AIs[1]
    table.cell(2, 1).text = str(val_AI2[0])
    table.cell(2, 2).text = str(val_AI2[1])
    table.cell(2, 3).text = str(val_AI2[2])

    table.cell(3, 0).text = name_AIs[2]
    table.cell(3, 1).text = str(val_AI3[0])
    table.cell(3, 2).text = str(val_AI3[1])
    table.cell(3, 3).text = str(val_AI3[2])

    # define chart data ---------------------
    chart_data = ChartData()
    chart_data.categories = name_objects
    chart_data.add_series(name_AIs[0], val_AI1)
    chart_data.add_series(name_AIs[1], val_AI2)
    chart_data.add_series(name_AIs[2], val_AI3)

    # add chart to slide --------------------
    x, y, cx, cy = Cm(0.7), Cm(3.5), Cm(24), Cm(8)

    graphic_frame = slide.shapes.add_chart(
        XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
        )

    chart = graphic_frame.chart

    chart.has_legend = True
    chart.legend.position = XL_LEGEND_POSITION.TOP
    chart.legend.include_in_layout = False

    value_axis = chart.value_axis
    value_axis.maximum_scale = 100.0

    value_axis.has_title = True
    value_axis.axis_title.has_text_frame = True
    value_axis.axis_title.text_frame.text = "False positive"
    value_axis.axis_title.text_frame.auto_size
    #fit_text(font_family='Calibri', max_size=18, bold=False, italic=False, font_file=None)

    prs.save('test_template.pptx')

そうすると以下のようにグラフと表を含んだスライドが出力される。

image_slidereport.png

これにもう少しデータの読み込み等を工夫すれば、急な報告事項の追加にも柔軟に対応できるだろう。

これで偉い人からAIの精度が向上していくのを毎日報告しろと言われても大丈夫。

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
845