python生成ppt的方法
本文主要介紹如何通過(guò)python生成ppt文件,以及借助ppt模板來(lái)生成ppt
環(huán)境
python 3
python-pptx
安裝
pip3 install python-pptx
將文字輸出到ppt
效果圖
代碼
from pptx import Presentation # 創(chuàng)建幻燈片 ------ 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] # 設(shè)置標(biāo)題和副標(biāo)題 title.text = "Hello, World!" subtitle.text = "pip install python-pptx" prs.save("test.pptx")
圖表輸出到ppt
效果圖
代碼
from pptx import Presentation from pptx.chart.data import ChartData from pptx.enum.chart import XL_CHART_TYPE from pptx.util import Inches # 創(chuàng)建幻燈片 ------ prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[5]) # 定義圖表數(shù)據(jù) --------------------- 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模板來(lái)生成ppt
- 準(zhǔn)備ppt模板(網(wǎng)絡(luò)下載或自定義幻燈片母版)
- 加載ppt模板,并使用指定幻燈片樣式
- 添加數(shù)據(jù)并生成新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__': # 創(chuàng)建幻燈片 ------ 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 = '報(bào)告' # 定義表格數(shù)據(jù) ------ 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 # 設(shè)置單元格寬度 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) # 設(shè)置標(biāo)題行 table.cell(0, 1).text = name_objects[0] table.cell(0, 2).text = name_objects[1] table.cell(0, 3).text = name_objects[2] # 填充數(shù)據(jù) 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]) # 定義圖表數(shù)據(jù) --------------------- 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')
本文用到的源碼
利用場(chǎng)景
- 周期性數(shù)據(jù)匯總及報(bào)表生成
- 業(yè)務(wù)或項(xiàng)目等數(shù)據(jù)可視化
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python 環(huán)境搭建 及python-3.4.4的下載和安裝過(guò)程
這篇文章主要介紹了python 環(huán)境搭建 python-3.4.4的下載和安裝過(guò)程,文中給大家補(bǔ)充介紹了pycharm的基本用法,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07python應(yīng)用Axes3D繪圖(批量梯度下降算法)
這篇文章主要為大家詳細(xì)介紹了python應(yīng)用Axes3D繪圖,批量梯度下降算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03使用OpenCV實(shí)現(xiàn)道路車(chē)輛計(jì)數(shù)的使用方法
這篇文章主要介紹了使用OpenCV實(shí)現(xiàn)道路車(chē)輛計(jì)數(shù)的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Pandas DataFrame分組求和、分組乘積的實(shí)例
這篇文章主要介紹了Pandas DataFrame分組求和、分組乘積的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02python 實(shí)現(xiàn)將小圖片放到另一個(gè)較大的白色或黑色背景圖片中
今天小編就為大家分享一篇python 實(shí)現(xiàn)將小圖片放到另一個(gè)較大的白色或黑色背景圖片中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12