使用Python實現(xiàn)在PowerPoint文件中創(chuàng)建各種類型的圖表
在現(xiàn)代商務(wù)演示和數(shù)據(jù)分析報告中,將復(fù)雜數(shù)據(jù)以可視化圖表的形式呈現(xiàn)在 PowerPoint 幻燈片中已成為標準做法。無論是銷售匯報、項目進度展示還是市場分析,programmatically 在 PowerPoint 中插入專業(yè)圖表都能顯著提升工作效率和演示質(zhì)量。本文將深入探討如何使用 Python 在 PowerPoint 演示文稿中創(chuàng)建各種類型的圖表,并對其進行精確的數(shù)據(jù)配置和樣式定制。
為什么選擇自動化創(chuàng)建 PowerPoint 圖表
傳統(tǒng)的手動在 PowerPoint 中創(chuàng)建圖表需要重復(fù)的點擊和配置,而通過 Python 編程方式實現(xiàn)這一過程具有以下顯著優(yōu)勢:
- 批量生成報告:一次性為不同部門或時間段創(chuàng)建數(shù)百份包含相同圖表結(jié)構(gòu)的演示文稿
- 數(shù)據(jù)動態(tài)更新:直接從數(shù)據(jù)庫、API 或 Excel 文件讀取數(shù)據(jù),確保圖表反映最新信息
- 格式標準化:保證所有演示文稿的圖表風(fēng)格、顏色和字體保持一致的企業(yè)形象
- 時間效率:將數(shù)小時的手動工作壓縮到幾分鐘內(nèi)自動完成
環(huán)境準備
在開始之前,需要安裝支持 PowerPoint 操作的 Python 庫。Spire.Presentation for Python 是一款專業(yè)的 PPT 開發(fā)組件,提供了全面的 API 來操作 PPTX 格式的演示文稿,包括圖表的創(chuàng)建、編輯和格式化。
pip install Spire.Presentation
安裝完成后,即可在 Python 腳本中導(dǎo)入相關(guān)模塊開始工作。
PowerPoint 圖表對象模型解析
在使用 Spire.Presentation 創(chuàng)建圖表前,需要了解其基本的對象層次結(jié)構(gòu):
- Presentation:表示整個 PowerPoint 演示文稿
- Slide:演示文稿中的單張幻燈片
- ShapeCollection:幻燈片上的形狀集合
- Chart:圖表對象,包含圖表數(shù)據(jù)、系列、坐標軸等屬性
- ChartData:圖表的數(shù)據(jù)源,以單元格矩陣形式組織
- Series:數(shù)據(jù)系列,代表圖表中的一組相關(guān)數(shù)據(jù)點
- Categories:類別標簽,通常是 X 軸的標識
基本操作流程為:創(chuàng)建演示文稿 → 訪問幻燈片 → 添加圖表形狀 → 填充圖表數(shù)據(jù) → 設(shè)置標題和標簽 → 保存演示文稿。
創(chuàng)建第一個圖表:簇狀柱形圖
簇狀柱形圖是最常用的 PowerPoint 圖表類型之一,適合比較多個系列的分類數(shù)據(jù)。以下示例展示了如何創(chuàng)建一個完整的簇狀柱形圖:
from spire.presentation.common import *
from spire.presentation import *
# 創(chuàng)建演示文稿對象
presentation = Presentation()
# 定義圖表的位置和大小(左,上,右,下)
rect1 = RectangleF.FromLTRB(90, 100, 640, 420)
# 添加簇狀柱形圖到第一張幻燈片
chart = presentation.Slides[0].Shapes.AppendChartInit(ChartType.ColumnClustered, rect1, False)
# 設(shè)置圖表標題
chart.ChartTitle.TextProperties.Text = "季度銷售對比"
chart.ChartTitle.TextProperties.IsCentered = True
chart.ChartTitle.Height = 30
chart.HasTitle = True
# 準備數(shù)據(jù)系列
Series1 = [7.7, 8.9, 1.0, 2.4]
Series2 = [15.2, 5.3, 6.7, 8.0]
# 設(shè)置系列名稱
chart.ChartData[0, 1].Text = "產(chǎn)品 A"
chart.ChartData[0, 2].Text = "產(chǎn)品 B"
# 設(shè)置類別標簽
chart.ChartData[1, 0].Text = "第一季度"
chart.ChartData[2, 0].Text = "第二季度"
chart.ChartData[3, 0].Text = "第三季度"
chart.ChartData[4, 0].Text = "第四季度"
# 填充數(shù)據(jù)值
i = 0
while i < len(Series1):
chart.ChartData[i + 1, 1].NumberValue = Series1[i]
chart.ChartData[i + 1, 2].NumberValue = Series2[i]
i += 1
# 設(shè)置系列標簽范圍
chart.Series.SeriesLabel = chart.ChartData["B1", "C1"]
# 設(shè)置類別標簽范圍
chart.Categories.CategoryLabels = chart.ChartData["A2", "A5"]
# 設(shè)置系列的值范圍
chart.Series[0].Values = chart.ChartData["B2", "B5"]
chart.Series[1].Values = chart.ChartData["C2", "C5"]
# 保存演示文稿
presentation.SaveToFile("ColumnChart.pptx", FileFormat.Pptx2010)
presentation.Dispose()
關(guān)鍵點解析:
AppendChartInit()方法接受三個參數(shù):圖表類型枚舉、矩形區(qū)域(定義位置和大?。⑹欠駷?3D 效果ChartData是一個二維數(shù)據(jù)表,第一行用于系列名稱,第一列用于類別標簽- 數(shù)據(jù)從第二行第二列開始填充,行列索引從 0 開始
SeriesLabel、CategoryLabels和Values定義了圖表數(shù)據(jù)的來源范圍
折線圖:趨勢分析的最佳選擇
折線圖擅長展示數(shù)據(jù)隨時間變化的趨勢,特別適合展示月度、季度或年度的數(shù)據(jù)變化模式:
from spire.presentation.common import *
from spire.presentation import *
presentation = Presentation()
# 創(chuàng)建折線圖
rect = RectangleF.FromLTRB(50, 80, 600, 400)
lineChart = presentation.Slides[0].Shapes.AppendChartInit(ChartType.Line, rect, False)
# 設(shè)置標題
lineChart.ChartTitle.TextProperties.Text = "年度收入趨勢"
lineChart.HasTitle = True
# 設(shè)置類別(月份)
months = ["一月", "二月", "三月", "四月", "五月", "六月"]
for i in range(len(months)):
lineChart.ChartData[i + 1, 0].Text = months[i]
# 設(shè)置系列名稱和數(shù)據(jù)
lineChart.ChartData[0, 1].Text = "2023 年"
lineChart.ChartData[0, 2].Text = "2024 年"
# 填充兩年的數(shù)據(jù)
data2023 = [120.5, 135.2, 148.7, 162.3, 178.9, 195.4]
data2024 = [145.3, 158.6, 172.1, 189.5, 205.8, 223.7]
for i in range(len(data2023)):
lineChart.ChartData[i + 1, 1].NumberValue = data2023[i]
lineChart.ChartData[i + 1, 2].NumberValue = data2024[i]
# 配置數(shù)據(jù)和標簽
lineChart.Series.SeriesLabel = lineChart.ChartData["B1", "C1"]
lineChart.Categories.CategoryLabels = lineChart.ChartData["A2", "A7"]
lineChart.Series[0].Values = lineChart.ChartData["B2", "B7"]
lineChart.Series[1].Values = lineChart.ChartData["C2", "C7"]
# 保存文件
presentation.SaveToFile("LineChart.pptx", FileFormat.Pptx2010)
presentation.Dispose()
折線圖的特點:
- 通過線條的起伏直觀展示數(shù)據(jù)的變化趨勢
- 支持多個系列對比,便于進行同期比較
- 適合時間序列數(shù)據(jù)的可視化呈現(xiàn)
餅圖:占比關(guān)系的直觀展示
餅圖是展示各部分占整體比例的理想選擇,在市場份額、預(yù)算分配等場景中廣泛應(yīng)用:
from spire.presentation.common import *
from spire.presentation import *
presentation = Presentation()
# 創(chuàng)建餅圖
rect = RectangleF.FromLTRB(100, 100, 550, 450)
pieChart = presentation.Slides[0].Shapes.AppendChartInit(ChartType.Pie, rect, False)
# 設(shè)置標題
pieChart.ChartTitle.TextProperties.Text = "產(chǎn)品銷售占比"
pieChart.HasTitle = True
# 設(shè)置產(chǎn)品類別
products = ["產(chǎn)品 A", "產(chǎn)品 B", "產(chǎn)品 C", "產(chǎn)品 D"]
for i in range(len(products)):
pieChart.ChartData[i + 1, 0].Text = products[i]
# 設(shè)置系列名稱和銷售額數(shù)據(jù)
pieChart.ChartData[0, 1].Text = "銷售額(萬元)"
salesData = [45.0, 30.0, 15.0, 10.0]
for i in range(len(salesData)):
pieChart.ChartData[i + 1, 1].NumberValue = salesData[i]
# 配置數(shù)據(jù)范圍
pieChart.Series.SeriesLabel = pieChart.ChartData["B1", "B1"]
pieChart.Categories.CategoryLabels = pieChart.ChartData["A2", "A5"]
pieChart.Series[0].Values = pieChart.ChartData["B2", "B5"]
# 保存演示文稿
presentation.SaveToFile("PieChart.pptx", FileFormat.Pptx2010)
presentation.Dispose()
餅圖的使用要點:
- 通常只使用一個數(shù)據(jù)系列
- 每個扇區(qū)的大小由該類別數(shù)值占總和的比例決定
- 適合展示 3-7 個類別的占比關(guān)系,類別過多會影響可讀性
條形圖:水平數(shù)據(jù)對比
條形圖是柱形圖的水平版本,特別適合類別名稱較長或需要強調(diào)排名關(guān)系的場景:
from spire.presentation.common import *
from spire.presentation import *
presentation = Presentation()
# 創(chuàng)建條形圖
rect = RectangleF.FromLTRB(80, 90, 620, 430)
barChart = presentation.Slides[0].Shapes.AppendChartInit(ChartType.BarClustered, rect, False)
# 設(shè)置標題
barChart.ChartTitle.TextProperties.Text = "部門績效評估"
barChart.HasTitle = True
# 設(shè)置部門名稱(類別)
departments = ["市場部", "銷售部", "研發(fā)部", "客服部", "財務(wù)部"]
for i in range(len(departments)):
barChart.ChartData[i + 1, 0].Text = departments[i]
# 設(shè)置系列和數(shù)據(jù)
barChart.ChartData[0, 1].Text = "得分"
scores = [85.5, 92.0, 88.5, 79.0, 95.0]
for i in range(len(scores)):
barChart.ChartData[i + 1, 1].NumberValue = scores[i]
# 配置數(shù)據(jù)范圍
barChart.Series.SeriesLabel = barChart.ChartData["B1", "B1"]
barChart.Categories.CategoryLabels = barChart.ChartData["A2", "A6"]
barChart.Series[0].Values = barChart.ChartData["B2", "B6"]
# 保存文件
presentation.SaveToFile("BarChart.pptx", FileFormat.Pptx2010)
presentation.Dispose()
條形圖的優(yōu)勢:
- 水平布局為長類別名稱提供充足的顯示空間
- 便于觀察排名順序,從左到右自然遞減
- 適合類別名稱差異較大的數(shù)據(jù)集
圖表尺寸和位置控制
圖表在幻燈片上的位置和大小直接影響演示的整體視覺效果。通過 RectangleF.FromLTRB() 方法可以精確控制圖表的區(qū)域:
# 定義圖表矩形區(qū)域(左,上,右,下) # 單位通常為點(point),1 點 = 1/72 英寸 rect = RectangleF.FromLTRB(90, 100, 640, 420) # 計算圖表的寬度和高度 # 寬度 = 右 - 左 = 640 - 90 = 550 點 # 高度 = 下 - 上 = 420 - 100 = 320 點 chart = presentation.Slides[0].Shapes.AppendChartInit(ChartType.ColumnClustered, rect, False)
定位技巧:
- 保持幻燈片邊緣留白,避免內(nèi)容過于擁擠
- 標準 16:9 幻燈片尺寸約為 960x540 點
- 常用圖表寬度:400-600 點,高度:300-400 點
- 考慮為標題、圖例和數(shù)據(jù)標簽預(yù)留空間
數(shù)據(jù)系列的高級管理
Spire.Presentation 提供了靈活的數(shù)據(jù)系列操作方法,支持復(fù)雜的數(shù)據(jù)配置需求:
多系列對比
# 設(shè)置三個系列名稱
chart.ChartData[0, 1].Text = "2022 年"
chart.ChartData[0, 2].Text = "2023 年"
chart.ChartData[0, 3].Text = "2024 年"
# 填充三年數(shù)據(jù)
for i in range(4): # 4 個季度
chart.ChartData[i + 1, 1].NumberValue = data2022[i]
chart.ChartData[i + 1, 2].NumberValue = data2023[i]
chart.ChartData[i + 1, 3].NumberValue = data2024[i]
# 設(shè)置系列值范圍(三列數(shù)據(jù))
chart.Series[0].Values = chart.ChartData["B2", "B5"]
chart.Series[1].Values = chart.ChartData["C2", "C5"]
chart.Series[2].Values = chart.ChartData["D2", "D5"]
動態(tài)數(shù)據(jù)填充
從外部數(shù)據(jù)源(如 Excel 或數(shù)據(jù)庫)讀取數(shù)據(jù)并填充到圖表:
# 假設(shè)從 Excel 讀取了數(shù)據(jù)
excel_data = {
"categories": ["Q1", "Q2", "Q3", "Q4"],
"series1": [150.5, 180.2, 165.8, 210.3],
"series2": [175.2, 195.6, 188.4, 225.7]
}
# 填充類別
for i, category in enumerate(excel_data["categories"]):
chart.ChartData[i + 1, 0].Text = category
# 填充數(shù)據(jù)系列
for i in range(len(excel_data["series1"])):
chart.ChartData[i + 1, 1].NumberValue = excel_data["series1"][i]
chart.ChartData[i + 1, 2].NumberValue = excel_data["series2"][i]
圖表標題和標簽格式化
圖表的可讀性很大程度上取決于標題和標簽的格式化設(shè)置:
# 設(shè)置標題文本和樣式
chart.ChartTitle.TextProperties.Text = "銷售業(yè)績分析"
chart.ChartTitle.TextProperties.FontName = "微軟雅黑"
chart.ChartTitle.TextProperties.FontSize = 16
chart.ChartTitle.TextProperties.IsCentered = True
chart.ChartTitle.Height = 40
# 啟用標題顯示
chart.HasTitle = True
# 設(shè)置數(shù)據(jù)標簽(顯示具體數(shù)值)
for series in chart.Series:
series.DataLabels.ValueVisible = True
series.DataLabels.ShowSeriesName = False
series.DataLabels.ShowCategoryName = False
標題格式化的關(guān)鍵屬性:
- TextProperties.Text:標題顯示的文本內(nèi)容
- FontName:字體名稱,支持中文字體
- FontSize:字體大?。c)
- IsCentered:是否居中對齊
- Height:標題區(qū)域的高度
完整實戰(zhàn)示例:銷售分析報告
以下是一個綜合示例,展示如何創(chuàng)建包含多種圖表的銷售分析演示文稿:
from spire.presentation.common import *
from spire.presentation import *
def create_sales_presentation():
# 創(chuàng)建演示文稿
ppt = Presentation()
# === 第一頁:柱形圖 ===
rect1 = RectangleF.FromLTRB(80, 80, 640, 420)
chart1 = ppt.Slides[0].Shapes.AppendChartInit(ChartType.ColumnClustered, rect1, False)
chart1.ChartTitle.TextProperties.Text = "季度銷售對比"
chart1.HasTitle = True
# 設(shè)置數(shù)據(jù)
quarters = ["Q1", "Q2", "Q3", "Q4"]
productA = [120, 150, 180, 220]
productB = [95, 110, 135, 165]
chart1.ChartData[0, 1].Text = "產(chǎn)品 A"
chart1.ChartData[0, 2].Text = "產(chǎn)品 B"
for i in range(4):
chart1.ChartData[i + 1, 0].Text = quarters[i]
chart1.ChartData[i + 1, 1].NumberValue = productA[i]
chart1.ChartData[i + 1, 2].NumberValue = productB[i]
chart1.Series.SeriesLabel = chart1.ChartData["B1", "C1"]
chart1.Categories.CategoryLabels = chart1.ChartData["A2", "A5"]
chart1.Series[0].Values = chart1.ChartData["B2", "B5"]
chart1.Series[1].Values = chart1.ChartData["C2", "C5"]
# === 第二頁:餅圖 ===
slide2 = ppt.Slides.Append()
rect2 = RectangleF.FromLTRB(100, 100, 550, 450)
chart2 = slide2.Shapes.AppendChartInit(ChartType.Pie, rect2, False)
chart2.ChartTitle.TextProperties.Text = "市場份額分布"
chart2.HasTitle = True
products = ["產(chǎn)品 A", "產(chǎn)品 B", "產(chǎn)品 C", "產(chǎn)品 D"]
shares = [40.0, 30.0, 20.0, 10.0]
chart2.ChartData[0, 1].Text = "份額%"
for i in range(4):
chart2.ChartData[i + 1, 0].Text = products[i]
chart2.ChartData[i + 1, 1].NumberValue = shares[i]
chart2.Series.SeriesLabel = chart2.ChartData["B1", "B1"]
chart2.Categories.CategoryLabels = chart2.ChartData["A2", "A5"]
chart2.Series[0].Values = chart2.ChartData["B2", "B5"]
# 保存演示文稿
ppt.SaveToFile("Sales_Analysis.pptx", FileFormat.Pptx2010)
ppt.Dispose()
create_sales_presentation()
生成結(jié)果預(yù)覽
以下是上述代碼生成的演示文稿:

常見問題與解決方案
圖表數(shù)據(jù)顯示異常
確保所有數(shù)值都轉(zhuǎn)換為正確的數(shù)據(jù)類型:
# 始終使用 NumberValue 設(shè)置數(shù)值 chart.ChartData[row, col].NumberValue = float(value) # 使用 Text 設(shè)置文本標簽 chart.ChartData[row, col].Text = str(label)
中文標簽亂碼
Spire.Presentation 完全支持 Unicode,可以直接使用中文字符串:
chart.ChartData[row, col].Text = "中文標簽" # 完全支持 chart.ChartTitle.TextProperties.Text = "圖表標題" # 支持中文
圖表位置偏移
調(diào)整矩形區(qū)域的坐標值來微調(diào)圖表位置:
# 向右移動 20 點,向下移動 10 點 rect = RectangleF.FromLTRB(110, 110, 660, 430)
總結(jié)
通過掌握 Spire.Presentation for Python 的圖表 API,開發(fā)者可以實現(xiàn) PowerPoint 演示文稿的完全自動化生成。本文介紹了五種常用圖表類型的創(chuàng)建方法,涵蓋了從基礎(chǔ)數(shù)據(jù)填充到高級格式化的完整流程。
核心要點回顧:
- 使用
AppendChartInit()方法創(chuàng)建圖表,指定類型和位置區(qū)域 - 通過
ChartData二維表組織數(shù)據(jù),第一行用于系列名,第一列用于類別 - 正確設(shè)置
SeriesLabel、CategoryLabels和Values范圍 - 利用
HasTitle和ChartTitle配置圖表標題 - 支持中文標簽和標題,滿足本土化需求
這些技能可以應(yīng)用于:
- 自動生成周期性業(yè)務(wù)報告
- 批量創(chuàng)建培訓(xùn)或教學(xué)材料
- 數(shù)據(jù)可視化儀表板制作
- 動態(tài)演示文稿生成系統(tǒng)
掌握了這些基礎(chǔ)后,你可以進一步探索更多高級功能,如 3D 圖表、組合圖表、趨勢線添加、數(shù)據(jù)標簽格式化等,構(gòu)建出功能強大的演示文稿自動化解決方案。
以上就是使用Python實現(xiàn)在PowerPoint文件中創(chuàng)建各種類型的圖表的詳細內(nèi)容,更多關(guān)于Python PowerPoint創(chuàng)建圖表的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python不要再使用while死循環(huán),定時器代替效果更佳
在python開發(fā)的過程中,經(jīng)常見到小伙伴直接使用while True的死循環(huán)+sleep的方式來保存程序的一直運行。這種方式雖然能達到效果,但是說不定什么時候就直接崩潰了,其實使用定時器效果也不錯哦2023-03-03
TensorFlow2中提供的幾種處理特征列的方法小結(jié)
本文主要介紹了TensorFlow2中提供的幾種處理特征列的方法小結(jié),主要介紹了6種方式,具有一定的參考價值,感興趣的可以了解一下2023-09-09
Python給函數(shù)加上狀態(tài)的多種方式小結(jié)
通常,函數(shù)是無狀態(tài)的:每次調(diào)用它都會從相同的初始狀態(tài)開始執(zhí)行,而有時候,我們希望函數(shù)在多次調(diào)用之間能夠保留某些信息,這種功能可以通過給函數(shù)加上狀態(tài)來實現(xiàn),所以本文給大家介紹了Python給函數(shù)加上狀態(tài)的多種方式,需要的朋友可以參考下2025-06-06

