python办公室自动化之office颜值担当-PPT
生活随笔
收集整理的這篇文章主要介紹了
python办公室自动化之office颜值担当-PPT
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
學習目標:
學習python-pptx模塊對office PPT 的基本操作學習內容:
1.python寫入文本到PPT中準備條件:
安裝python-pptx模塊 pip install python-pptx官方文檔:https://python-pptx.readthedocs.io/en/latest/api/presentation.html#presentation-function
學習記錄:
1.導入模塊
import pptx from pptx.util import Inches from pptx.enum.shapes import MSO_SHAPE from pptx.chart.data import CategoryChartData from pptx.enum.chart import XL_CHART_TYPE, XL_LEGEND_POSITION2.得到演示文稿對象(首次創建)
prx = pptx.Presentation()3.在某個ppt模板基礎上進行修改,獲取演示文稿對象
prx = pptx.Presentation('test.pptx')4.創建幻燈片
slide1 = prx.slides.add_slide(prx.slide_layouts[0]) slide2 = prx.slides.add_slide(prx.slide_layouts[1]) slide3 = prx.slides.add_slide(prx.slide_layouts[2]) slide5 = prx.slides.add_slide(prx.slide_layouts[4]) slide6 = prx.slides.add_slide(prx.slide_layouts[5])5.獲取幻燈片總數量
print(len(prx.slides))6.刪除第二張幻燈片
del prx.slides._sldIdLst[1]7.插入文本內容
text1 = slide1.shapes.add_textbox(Inches(5),Inches(5),Inches(5),Inches(5)) #距離左側頂部的距離為5英寸,設置的文本框寬高為5英寸 text1.text = '我是文本框' #添加段落 p1 =text1.text_frame.add_paragraph() p1.text = '我是段落' p1.add_run().text='End' #添加標題 title_shape = slide1.shapes.title title_shape.text = '我是標題1' slide1.shapes.placeholders[1].text='我是標題2'9.插入自選圖形-矩形
slide3.shapes.add_shape(MSO_SHAPE.RECTANGLE,Inches(2),Inches(2),Inches(5),Inches(3))10.插入表格
#添加表格 rows = 3 cols = 3 left = Inches(2) top = Inches(2) width = Inches(4) height = Inches(2) table = slide5.shapes.add_table(rows,cols,left,top,width,height).table #填充內容 table.cell(1,0).text = '姓名' table.cell(1,1).text = '年齡' table.cell(1,2).text = '語言' table.cell(2,0).text = '張三' table.cell(2,1).text = '28' table.cell(2,2).text = 'Python' #合并單元格 cell0 = table.cell(0,0) cell1 = table.cell(0,2) cell0.merge(cell1) cell0.text = '班級學生信息'11.插入圖表
#寫入圖表信息 chart_data = CategoryChartData() chart_data.categories = ['一月份','二月份','三月份'] #x軸 chart_data.add_series('Y2020',(300,400,500)) chart_data.add_series('Y2021',(500,600,700)) chart = slide6.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED,Inches(2),Inches(2),Inches(6),Inches(4),chart_data).chart chart.has_title = True chart.chart_title.text_frame.text = '第一季度銷售額' #設置圖例 chart.has_legend = True chart.legend.position = XL_LEGEND_POSITION.RIGHT12.保存幻燈片
prx.save('test.pptx')總結
以上是生活随笔為你收集整理的python办公室自动化之office颜值担当-PPT的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Amazon Deep Learni
- 下一篇: 哈哈2333