溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python可以寫PPT嗎

發布時間:2020-07-17 11:06:55 來源:億速云 閱讀:205 作者:清晨 欄目:編程語言

小編給大家分享一下python可以寫PPT嗎,相信大部分人都還不怎么了解,因此分享這邊文章給大家學習,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去學習方法吧!

python可以寫PPT。方法為:1、輸入“pip3 install python-pptx”命令安裝python-pptx;2、準備ppt模板(網絡下載或自定義幻燈片);3、加載ppt模板并使用指定幻燈片樣式;4、添加數據即可生成ppt。

python可以寫PPT嗎

簡介

本文主要介紹如何通過python生成ppt文件,以及借助ppt模板來生成ppt

環境

python 3

python-pptx

安裝

pip3 install python-pptx

將文字輸出到ppt

效果圖

python可以寫PPT嗎

代碼

from pptx import Presentation
# 創建幻燈片 ------
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
# 設置標題和副標題
title.text = "Hello, World!"
subtitle.text = "pip install python-pptx"
prs.save("test.pptx")

圖表輸出到ppt

效果圖

python可以寫PPT嗎

代碼

from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
# 創建幻燈片 ------
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
# 定義圖表數據 ---------------------
chart_data = ChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))
# 將圖表添加到幻燈片 --------------------
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
slide.shapes.add_chart(
    XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
) 
prs.save('chart-01.pptx')

使用ppt模板來生成ppt

準備ppt模板(網絡下載或自定義幻燈片母版)

加載ppt模板,并使用指定幻燈片樣式

添加數據并生成新ppt

效果圖

python可以寫PPT嗎

代碼

from pptx import Presentation
from pptx.util import Inches
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Cm #Inches
from pptx.enum.chart import XL_LEGEND_POSITION
if __name__ == '__main__':
    # 創建幻燈片 ------
    prs = Presentation('template.pptx')
    title_only_slide_layout = prs.slide_layouts[5]
    slide = prs.slides.add_slide(title_only_slide_layout)
    shapes = slide.shapes
    shapes.title.text = '報告'
 
    # 定義表格數據 ------
    name_objects = ["object1", "object2", "object3"]
    name_AIs = ["AI1", "AI2", "AI3"]
    val_AI1 = (19.2, 21.4, 16.7)
    val_AI2 = (22.3, 28.6, 15.2)
    val_AI3 = (20.4, 26.3, 14.2)
    val_AIs = [val_AI1, val_AI2, val_AI3]
 
    # 表格樣式 --------------------
    rows = 4
    cols = 4
    top    = Cm(12.5)
    left   = Cm(3.5) #Inches(2.0)
    width  = Cm(24) # Inches(6.0)
    height = Cm(6) # Inches(0.8)
 
    # 添加表格到幻燈片 --------------------
    table = shapes.add_table(rows, cols, left, top, width, height).table
 
    # 設置單元格寬度
    table.columns[0].width = Cm(6)# Inches(2.0)
    table.columns[1].width = Cm(6)
    table.columns[2].width = Cm(6)
    table.columns[3].width = Cm(6)
 
    # 設置標題行
    table.cell(0, 1).text = name_objects[0]
    table.cell(0, 2).text = name_objects[1]
    table.cell(0, 3).text = name_objects[2]
 
    # 填充數據
    table.cell(1, 0).text = name_AIs[0]
    table.cell(1, 1).text = str(val_AI1[0])
    table.cell(1, 2).text = str(val_AI1[1])
    table.cell(1, 3).text = str(val_AI1[2])
 
    table.cell(2, 0).text = name_AIs[1]
    table.cell(2, 1).text = str(val_AI2[0])
    table.cell(2, 2).text = str(val_AI2[1])
    table.cell(2, 3).text = str(val_AI2[2])
 
    table.cell(3, 0).text = name_AIs[2]
    table.cell(3, 1).text = str(val_AI3[0])
    table.cell(3, 2).text = str(val_AI3[1])
    table.cell(3, 3).text = str(val_AI3[2])
 
    # 定義圖表數據 ---------------------
    chart_data = ChartData()
    chart_data.categories = name_objects
    chart_data.add_series(name_AIs[0], val_AI1)
    chart_data.add_series(name_AIs[1], val_AI2)
    chart_data.add_series(name_AIs[2], val_AI3)
 
    # 添加圖表到幻燈片 --------------------
    x, y, cx, cy = Cm(3.5), Cm(4.2), Cm(24), Cm(8)
 
    graphic_frame = slide.shapes.add_chart(
        XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
        )
 
    chart = graphic_frame.chart
 
    chart.has_legend = True
    chart.legend.position = XL_LEGEND_POSITION.TOP
    chart.legend.include_in_layout = False
 
    value_axis = chart.value_axis
    value_axis.maximum_scale = 100.0
 
    value_axis.has_title = True
    value_axis.axis_title.has_text_frame = True
    value_axis.axis_title.text_frame.text = "False positive"
    value_axis.axis_title.text_frame.auto_size
 
    prs.save('test_template.pptx')

以上是python可以寫PPT嗎的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

pp
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女