欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python讀取Excel數(shù)據(jù)在PPT中創(chuàng)建圖表

 更新時間:2024年08月19日 10:03:11   作者:Eiceblue  
使用Python從Excel讀取數(shù)據(jù)并在PowerPoint幻燈片中創(chuàng)建圖表不僅能夠極大地簡化圖表創(chuàng)建過程,通過Python這一橋梁,我們可以輕松實(shí)現(xiàn)數(shù)據(jù)自動化處理和圖表生成,本文將演示如何使用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 PythonSpire.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)建圖表。
操作步驟示例:

  1. 導(dǎo)入所需模塊。
  2. 創(chuàng)建Presentation實(shí)例,使用Presentation.SlideSize.Type屬性設(shè)置幻燈片大小。
  3. 創(chuàng)建Workbook實(shí)例,并使用Workbook.LoadFromFile()載入Excel文件。
  4. 使用ISlide.Shapes.AppendChart()方法在默認(rèn)幻燈片中創(chuàng)建一個圖表,并使用IChart.ChartData.Clear(0, 0, 5, 5)方法清除圖表的示例數(shù)據(jù)。
  5. 通過Worksheet.AllocatedRange[].Text讀取表頭和列頭文本,并通過IChart.ChartData[].Text屬性將其設(shè)置為圖表數(shù)據(jù)的表頭和列頭。
  6. 遍歷工作表的數(shù)據(jù)行和數(shù)據(jù)列,使用Worksheet.AllocatedRange[].NumberValue屬性讀取數(shù)據(jù),并通過IChart.ChartData[].NumberValue將其設(shè)置為圖表數(shù)據(jù)對應(yīng)單元格的數(shù)據(jù)。
  7. 使用IChart.ChartTitle下的屬性設(shè)置圖表標(biāo)題。
  8. 使用IChart.Series.SeriesLabelIChart.Categories.CategoryLabels設(shè)置圖表系列和類別對應(yīng)的單元格范圍。
  9. 使用IChart.Series.get_Item().Values設(shè)置系列對應(yīng)的數(shù)據(jù)單元格范圍。
  10. 使用Worksheet.AllocatedRange[].NumberFormat屬性讀取Excel數(shù)據(jù)單元格的數(shù)字格式,并通過IChart.PrimaryValueAxis.NumberFormat將其設(shè)置為圖表縱坐標(biāo)的數(shù)字格式。
  11. 設(shè)置圖表重疊和間隔寬度。
  12. 使用Presentation.SaveToFile()保存演示文稿。
  13. 釋放資源。

代碼示例

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)。

操作示例:

  1. 導(dǎo)入所需模塊。
  2. 創(chuàng)建Presentation實(shí)例,設(shè)置幻燈片大小。
  3. 創(chuàng)建Workbook實(shí)例,使用Workbook.LoadFromFile()載入Excel文件。
  4. 使用Workbook.Worksheets.get_Item()方法獲取第一個工作表。
  5. 使用Workbook.SaveChartAsImage(worksheet, 0).ToArray()方法將工作表中的第一個圖表保存為數(shù)組,并轉(zhuǎn)換為圖片流。
  6. 使用Presentation.Images.AppendStream()方法將圖片流作為圖片數(shù)據(jù)嵌入到演示文稿中。
  7. 使用ISlide.Shapes.AppendEmbedImageByImageData()方法將圖片插入到幻燈片中。
  8. 使用Presentation.SaveToFile()方法保存演示文稿。
  9. 釋放資源。

代碼示例

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)文章

最新評論