使用Python讀取Excel數(shù)據(jù)在PPT中創(chuàng)建圖表
引言
可視化數(shù)據(jù)已成為提高演示文稿專業(yè)度的關(guān)鍵因素之一。使用Python從Excel讀取數(shù)據(jù)并在PowerPoint幻燈片中創(chuàng)建圖表不僅能夠極大地簡化圖表創(chuàng)建過程,還能確保數(shù)據(jù)的準(zhǔn)確性和圖表的即時性。通過Python這一橋梁,我們可以輕松實(shí)現(xiàn)數(shù)據(jù)自動化處理和圖表生成,進(jìn)而提升演示文稿的質(zhì)量和效率。本文將演示如何使用Python讀取Excel數(shù)據(jù)在PPT中創(chuàng)建圖表,以及將Excel圖表以圖片形式插入到幻燈片中。
本文所用的方法需要用到Spire.Presentation for Python和Spire.XLS for Python,PyPI:pip install Spire.Presentation Spire.XLS
。
讀取Excel數(shù)據(jù)在PPT中創(chuàng)建圖表
我們可以通過讀取Excel工作表數(shù)據(jù),然后在幻燈片中創(chuàng)建一個圖表并將讀取數(shù)據(jù)設(shè)置為圖表的數(shù)據(jù),最后進(jìn)行縱橫坐標(biāo)設(shè)置,來實(shí)現(xiàn)讀取Excel數(shù)據(jù)在演示文稿中創(chuàng)建圖表。
操作步驟示例:
- 導(dǎo)入所需模塊。
- 創(chuàng)建
Presentation
實(shí)例,使用Presentation.SlideSize.Type
屬性設(shè)置幻燈片大小。 - 創(chuàng)建
Workbook
實(shí)例,并使用Workbook.LoadFromFile()
載入Excel文件。 - 使用
ISlide.Shapes.AppendChart()
方法在默認(rèn)幻燈片中創(chuàng)建一個圖表,并使用IChart.ChartData.Clear(0, 0, 5, 5)
方法清除圖表的示例數(shù)據(jù)。 - 通過
Worksheet.AllocatedRange[].Text
讀取表頭和列頭文本,并通過IChart.ChartData[].Text
屬性將其設(shè)置為圖表數(shù)據(jù)的表頭和列頭。 - 遍歷工作表的數(shù)據(jù)行和數(shù)據(jù)列,使用
Worksheet.AllocatedRange[].NumberValue
屬性讀取數(shù)據(jù),并通過IChart.ChartData[].NumberValue
將其設(shè)置為圖表數(shù)據(jù)對應(yīng)單元格的數(shù)據(jù)。 - 使用
IChart.ChartTitle
下的屬性設(shè)置圖表標(biāo)題。 - 使用
IChart.Series.SeriesLabel
和IChart.Categories.CategoryLabels
設(shè)置圖表系列和類別對應(yīng)的單元格范圍。 - 使用
IChart.Series.get_Item().Values
設(shè)置系列對應(yīng)的數(shù)據(jù)單元格范圍。 - 使用
Worksheet.AllocatedRange[].NumberFormat
屬性讀取Excel數(shù)據(jù)單元格的數(shù)字格式,并通過IChart.PrimaryValueAxis.NumberFormat
將其設(shè)置為圖表縱坐標(biāo)的數(shù)字格式。 - 設(shè)置圖表重疊和間隔寬度。
- 使用
Presentation.SaveToFile()
保存演示文稿。 - 釋放資源。
代碼示例
from spire.presentation import Presentation, FileFormat, SlideSizeType, RectangleF, ChartType, ChartStyle, FillFormatType from spire.xls import Workbook # 創(chuàng)建Presentation實(shí)例 presentation = Presentation() # 設(shè)置幻燈片尺寸 presentation.SlideSize.Type = SlideSizeType.Screen16x9 # 創(chuàng)建Workbook實(shí)例 workbook = Workbook() # 加載Excel文件 workbook.LoadFromFile("示例.xlsx") # 獲取第一個工作表 sheet = workbook.Worksheets.get_Item(0) # 向第一張幻燈片添加圖表 rect = RectangleF.FromLTRB(50, 100, presentation.SlideSize.Size.Width - 50, presentation.SlideSize.Size.Height - 50) slide = presentation.Slides.get_Item(0) chart = slide.Shapes.AppendChart(ChartType.Area, rect) # 清除默認(rèn)的虛擬數(shù)據(jù) chart.ChartData.Clear(0, 0, 5, 5) # 將工作表的標(biāo)題列和標(biāo)題行復(fù)制到圖表 for i in range(sheet.AllocatedRange.ColumnCount): chart.ChartData[0, i].Text = sheet.AllocatedRange[1, i + 1].Text for j in range(sheet.AllocatedRange.RowCount - 1): chart.ChartData[j + 1, 0].Text = sheet.AllocatedRange.get_Item(j + 2, 1).Text # 遍歷數(shù)據(jù)行 for k in range(sheet.AllocatedRange.RowCount - 1): # 遍歷數(shù)據(jù)列 for l in range(sheet.AllocatedRange.ColumnCount - 1): # 設(shè)置圖表數(shù)據(jù) chart.ChartData[k + 1, l + 1].NumberValue = sheet.AllocatedRange[k + 2, l + 2].NumberValue # 設(shè)置圖表標(biāo)題 chart.ChartTitle.TextProperties.Text = sheet.Name chart.ChartTitle.TextProperties.IsCentered = True chart.ChartTitle.Height = 30 chart.HasTitle = True # 設(shè)置系列標(biāo)簽和類別標(biāo)簽 chart.Series.SeriesLabel = chart.ChartData["B1", "C1"] chart.Categories.CategoryLabels = chart.ChartData["A2", "A" + str(sheet.AllocatedRange.RowCount)] # 設(shè)置系列值 chart.Series.get_Item(0).Values = chart.ChartData["B2", "B" + str(sheet.AllocatedRange.RowCount)] chart.Series.get_Item(1).Values = chart.ChartData["C2", "C" + str(sheet.AllocatedRange.RowCount)] # 設(shè)置數(shù)據(jù)軸的數(shù)字格式 chart.PrimaryValueAxis.NumberFormat = sheet.AllocatedRange[2, 2].NumberFormat # 設(shè)置圖表樣式 chart.ChartStyle = ChartStyle.Style5 # 設(shè)置重疊和間距寬度 chart.OverLap = 50 chart.GapWidth = 200 # 保存演示文稿 presentation.SaveToFile("output/PresentationChartFromExcelData.pptx", FileFormat.Pptx2019) presentation.Dispose() workbook.Dispose()
結(jié)果演示文稿
將Excel圖表以圖片形式插入到幻燈片
如果想要將Excel中已有的圖表插入到PowerPoint演示文稿,并在最大限度上保持原有格式和外觀,可以通過將Excel圖表以圖片形式插入幻燈片來實(shí)現(xiàn)。
操作示例:
- 導(dǎo)入所需模塊。
- 創(chuàng)建
Presentation
實(shí)例,設(shè)置幻燈片大小。 - 創(chuàng)建
Workbook
實(shí)例,使用Workbook.LoadFromFile()
載入Excel文件。 - 使用
Workbook.Worksheets.get_Item()
方法獲取第一個工作表。 - 使用
Workbook.SaveChartAsImage(worksheet, 0).ToArray()
方法將工作表中的第一個圖表保存為數(shù)組,并轉(zhuǎn)換為圖片流。 - 使用
Presentation.Images.AppendStream()
方法將圖片流作為圖片數(shù)據(jù)嵌入到演示文稿中。 - 使用
ISlide.Shapes.AppendEmbedImageByImageData()
方法將圖片插入到幻燈片中。 - 使用
Presentation.SaveToFile()
方法保存演示文稿。 - 釋放資源。
代碼示例
from spire.presentation import Presentation, FileFormat, SlideSizeType, RectangleF, ShapeType, Stream from spire.xls import Workbook # 創(chuàng)建Presentation實(shí)例 presentation = Presentation() # 設(shè)置幻燈片尺寸 presentation.SlideSize.Type = SlideSizeType.Screen16x9 # 創(chuàng)建Workbook實(shí)例 workbook = Workbook() # 加載Excel文件 workbook.LoadFromFile("示例.xlsx") # 獲取第一個工作表 worksheet = workbook.Worksheets.get_Item(0) # 將第一個工作表中的第一個圖表保存為圖像 imageStream = Stream(workbook.SaveChartAsImage(worksheet, 0).ToArray()) # 將圖像嵌入到演示文稿中 imageData = presentation.Images.AppendStream(imageStream) # 獲取第一張幻燈片 slide = presentation.Slides.get_Item(0) # 向幻燈片添加圖像形狀 rect = RectangleF.FromLTRB(50, 100, presentation.SlideSize.Size.Width - 50, presentation.SlideSize.Size.Height - 50) slide.Shapes.AppendEmbedImageByImageData(ShapeType.Rectangle, imageData, rect) # 保存演示文稿 presentation.SaveToFile("output/InsertExcelChartToPresentation.pptx", FileFormat.Pptx2019) presentation.Dispose() workbook.Dispose()
結(jié)果演示文稿
本文介紹了如何使用Python讀取Excel數(shù)據(jù)在PowerPoint演示文稿中創(chuàng)建圖表,以及將Excel圖表以圖片形式插入到幻燈片中。
以上就是使用Python讀取Excel數(shù)據(jù)在PPT中創(chuàng)建圖表的詳細(xì)內(nèi)容,更多關(guān)于Python在PPT中創(chuàng)建圖表的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python使用Matplotlib模塊時坐標(biāo)軸標(biāo)題中文及各種特殊符號顯示方法
這篇文章主要介紹了Python使用Matplotlib模塊時坐標(biāo)軸標(biāo)題中文及各種特殊符號顯示方法,結(jié)合具體實(shí)例分析了Python使用Matplotlib模塊過程中針對中文及特殊符號的顯示方法,需要的朋友可以參考下2018-05-05Python批量將圖片灰度化的實(shí)現(xiàn)代碼
這篇文章主要介紹了Python批量將圖片灰度化的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04詳解基于Jupyter notebooks采用sklearn庫實(shí)現(xiàn)多元回歸方程編程
這篇文章主要介紹了詳解基于Jupyter notebooks采用sklearn庫實(shí)現(xiàn)多元回歸方程編程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Python Pandas實(shí)現(xiàn)數(shù)據(jù)分組求平均值并填充nan的示例
今天小編就為大家分享一篇Python Pandas實(shí)現(xiàn)數(shù)據(jù)分組求平均值并填充nan的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07pandas如何將DataFrame?轉(zhuǎn)為txt文本去除引號
這篇文章主要介紹了pandas如何將DataFrame?轉(zhuǎn)為txt文本去除引號,文中補(bǔ)充介紹了DataFrame導(dǎo)CSV?txt?||?每行有雙引號的原因及解決辦法,感興趣的朋友跟隨小編一起看看吧2024-01-01pandas按條件篩選數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了pandas按條件篩選數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02