本記事では、Pythonにて複数の画像ファイルをMicrosoft OfficeのPowerPointに貼り付ける雛形コードを載せました。1スライドあたりに貼り付ける列数を指定することで、画像サイズを自動で調整します。そして、貼り付ける行方向の枚数を自動で算出して、1スライドで収まらない場合は次のスライドへ順次貼り付けてゆく仕様です。使用するライブラリは「python-pptx」と「Pillow」です。
下図4つは、本コードの実行例です。貼り付ける画像の列方向の数を本コード内にある変数「num_cols」の数値で指定できます。
▼画像配置が1スライド4列の場合
各ページの上部には、タイトルとサブタイトルを入力できるレイアウトです。

▼画像配置が1スライド3列の場合

▼画像配置が1スライド2列の場合

▼画像配置が1スライド1枚の場合

■ライブラリのインストール
pip install python-pptx
pip install Pillow
■本プログラム
import glob
from pptx import Presentation
from pptx.util import Inches, Emu, Pt
from pptx.enum.text import PP_ALIGN
from pptx.dml.color import RGBColor
from pptx.enum.shapes import MSO_SHAPE
from PIL import Image
image_paths = glob.glob("./dir_img/*.jpg")
num_figs = len(image_paths)
print(num_figs, image_paths)
num_cols = 2
save_file_name = f"{num_cols}_cols.pptx"
def get_image_size(image_path):
with Image.open(image_path) as img:
width, height = img.size
return width, height
image_width, image_height = get_image_size(image_paths[0])
original_aspect_ratio = image_height / image_width
presentation = Presentation()
slide_width = presentation.slide_width - (2 * Inches(0.5))
slide_height = presentation.slide_height - (2 * Inches(0.5))
row_spacing = Inches(0.3)
col_spacing = Inches(0.3)
top_space_first_row = Inches(1.5)
side_space = Inches(1)
if (len(image_paths) % num_cols) == 0:
num_rows = len(image_paths) // num_cols
else:
num_rows = (len(image_paths) // num_cols) + 1
total_side_space = 2 * side_space
image_and_spacing_width = num_cols * (slide_width - total_side_space) \
+ (num_cols - 1) * col_spacing
if image_and_spacing_width > slide_width:
total_col_spacing = (num_cols - 1) * col_spacing
adjusted_image_width = (slide_width - total_side_space - total_col_spacing) / num_cols
adjusted_image_height = adjusted_image_width * original_aspect_ratio
for i in range(1, num_rows+1, 1):
image_and_spacing_height = adjusted_image_height * i + top_space_first_row + row_spacing*(i-1)
height_delta = slide_height - image_and_spacing_height
if height_delta < 0:
num_rows = i
break
elif num_cols == 1:
total_col_spacing = (num_cols - 1) * col_spacing
adjusted_image_width = (slide_width - total_side_space - total_col_spacing) / num_cols
adjusted_image_height = adjusted_image_width * original_aspect_ratio
num_rows = 1
print('num_cols, num_rows', num_cols, num_rows)
if (num_figs % (num_rows * num_cols)) == 0:
pages = num_figs // (num_rows * num_cols)
else:
pages = num_figs // (num_rows * num_cols) + 1
slide_count = 0
fig_count = 0
while slide_count < pages:
slide = presentation.slides.add_slide(presentation.slide_layouts[5])
title_shape = slide.shapes.title
title_shape.text = "タイトル"
title_shape.text_frame.paragraphs[0].alignment = PP_ALIGN.LEFT
title_shape.text_frame.paragraphs[0].font.size = Pt(32)
title_shape.top = Inches(0.1)
title_shape.width = presentation.slide_width
title_shape.height = title_shape.text_frame.paragraphs[0].font.size
left = title_shape.left
top = title_shape.top + title_shape.height
width = title_shape.width
height = Inches(0.5)
textbox = slide.shapes.add_textbox(left, top, width, height)
frame = textbox.text_frame
frame.text = "サブタイトル"
frame.paragraphs[0].alignment = PP_ALIGN.CENTER
frame.paragraphs[0].runs[0].font.size = Pt(28)
left = title_shape.left
top = title_shape.top + title_shape.height
width = title_shape.width
height = Pt(1)
line = slide.shapes.add_shape(
MSO_SHAPE.RECTANGLE, left, top, width, height
)
fill = line.fill
fill.solid()
fill.fore_color.rgb = RGBColor(0x00, 0x00, 0x00)
line.shadow.inherit = True
if num_rows != 1:
for row in range(num_rows):
for col in range(num_cols):
idx = row * num_cols + col
left_inch = col * (adjusted_image_width + col_spacing) + side_space
top_inch = top_space_first_row \
+ row * (adjusted_image_width * original_aspect_ratio + row_spacing)
fig_count = idx + slide_count*(num_rows*num_cols)
picture = slide.shapes.add_picture(image_paths[fig_count],
left_inch,
top_inch,
width=adjusted_image_width)
picture.lock_aspect_ratio = True
if (idx == (num_rows*num_cols)-1) or (fig_count == num_figs-1):
break
else:
continue
break
else:
left_inch = side_space
top_inch = top_space_first_row
picture = slide.shapes.add_picture(image_paths[slide_count],
left_inch,
top_inch,
width=adjusted_image_width)
picture.lock_aspect_ratio = True
slide_count += 1
presentation.save(save_file_name)
print(f"save {save_file_name}")
以上
<広告>
リンク
リンク
リンク