2017年7月16日日曜日

【python-pptx】PythonでPowerPointに線を引く。

目的

PythonでPowerPointを操る際に、線を引きたかったが、直接的にそれを行っているブログ等が見つけられなかったので、見つけられるように記事に残す。

環境

  • Windows 10
  • Python 3.6.1
  • python-pptx 0.6.6 

環境の構築方法は、他サイトを参考に。

ソースコード

### ファイル名 adding_an_line.py
from pptx import Presentation
from pptx.enum.shapes import MSO_CONNECTOR
from pptx.util import Inches
# PowerPoint
prs = Presentation()
# レイアウトの種類
BLANK = 6
# レイアウト
slide_layout = prs.slide_layouts[BLANK]
# スライド
slide = prs.slides.add_slide(slide_layout)
# 図形
shapes = slide.shapes

# 座標をインチに変換する
begin_x = 0
begin_y = 0
end_x   = Inches(1.0)
end_y   = Inches(1.0)

###
# 引数
#  引数1:MSO_CONNECTOR.STRAIGHT
#  引数2: begin x 座標
#  引数3: begin y 座標
#  引数4: end   x 座標
#  引数5  end   y 座標
###
shape = shapes.add_connector(
    MSO_CONNECTOR.STRAIGHT, begin_x, begin_y, end_x, end_y
  )
# PowerPointをカレントに保存
prs.save('adding_a_line.pptx')

実行方法

python adding_an_line.py

実行結果

ローカルに出力された「adding_a_line.pptx」に以下のように線が引かれる。