0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで表をPowerPointプレゼンテーションに挿入する方法

Posted at

効果的な情報伝達はプレゼンテーションの要点であり、PowerPointは最も広く使用されているプレゼンテーションツールの一つとして、発表者がこの目標を達成するための豊富な機能を提供します。その中でも、プレゼンテーションに表を挿入することで、データの理解や情報の比較がより直感的に行えます。Pythonのような強力なプログラミング言語を使用することで、表の作成を自動化し、PowerPointに挿入することができ、データの正確性を確保し、作業フローを簡素化できます。この記事では、Pythonを使用してPowerPointプレゼンテーションに表を追加する方法を紹介します。

この記事で使用する方法には、Spire.Presentation for Pythonが必要です。PyPIからインストールします:pip install Spire.Presentation

PythonでPowerPointプレゼンテーションに表を作成する

ISlide.Shapes.AppendTable(x: float, y: float, widths: list[float], heights: list[float])メソッドを使用すると、指定したスライドに直接表を作成できます。このメソッドを使用する際には、まず表の開始座標を決定し、データに基づいて各行の高さと列の幅を計算します。
次の手順で、CSVデータを読み込み、データに基づいてプレゼンテーションに表を作成し、簡単な表のフォーマット設定を行います:

  1. 必要なモジュールをインポートします。
  2. Presentationオブジェクトを作成します。
  3. CSVファイルデータを2次元の文字列リストとして読み込みます。
  4. Presentation.SlideSize.Typeメソッドを使用してスライドサイズを設定します。
  5. Presentation.Slides.get_Item()メソッドを使用してスライドを取得します。
  6. 表の座標と幅・高さを計算します。
  7. ISlide.Shapes.AppendTable()メソッドを使用して、指定したサイズと位置に表を作成します。
  8. セルのフォーマット設定を行います。
  9. ITable.StylePresetを使用して表のスタイルを設定します。
  10. Presentation.SaveToFile()メソッドを使用してプレゼンテーションを保存します。
  11. リソースを解放します。

コード例

from spire.presentation import Presentation, TextAlignmentType, TableStylePreset, FileFormat, TextFont, SlideSizeType
import csv

# Presentationオブジェクトを作成
presentation = Presentation()

# スライドサイズを設定
presentation.SlideSize.Type = SlideSizeType.Screen16x9

# デフォルトのスライドを取得
slide = presentation.Slides.get_Item(0)

# CSVファイルデータを2次元の文字列リストとして読み込む
with open("Sample.csv", "r", encoding="utf-8") as f:
    csv_reader = csv.reader(f)
    data = list(csv_reader)

# 表の座標と幅・高さを計算
rows = len(data)
columns = len(data[0])
width = float((presentation.SlideSize.Size.Width - 100) / columns)
height = float((presentation.SlideSize.Size.Height - 300) / rows)
widths = [width for _ in range(columns)]
heights = [height for _ in range(rows)]

# 表を追加
table = slide.Shapes.AppendTable(50, 200, widths, heights)
for i in range(rows):
    for j in range(columns):
        table.get_Item(j, i).TextFrame.Text = data[i][j]
        table.get_Item(j, i).TextFrame.Paragraphs.get_Item(0).FirstTextRange.LatinFont = TextFont("Yu Gothic UI")
        table.get_Item(j, i).TextFrame.TextRange.FontHeight = 24

# 表ヘッダをセンタリング
for i in range(columns):
    table.get_Item(i, 0).TextFrame.Paragraphs.get_Item(0).Alignment = TextAlignmentType.Center

# 表のスタイルを設定
table.StylePreset = TableStylePreset.ThemedStyle1Accent1

# プレゼンテーションを保存
presentation.SaveToFile("output/PowerPointプレゼンテーションにCSVテーブルを挿入.pptx", FileFormat.Pptx2013)
presentation.Dispose()

挿入された表
PowerPointプレゼンテーションにCSVテーブルを挿入

この記事では、PowerPointプレゼンテーションに特定のデータを含む表を追加する方法を紹介しました。

さらにPowerPointプレゼンテーションの処理に関するヒントについては、Spire.Presentation for Pythonのチュートリアルをご覧ください。

無料ライセンス申請

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?