poj 2480 python_python-pptx:是一个python处理ppt的库
生活随笔
收集整理的這篇文章主要介紹了
poj 2480 python_python-pptx:是一个python处理ppt的库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 說明
=====
1.1 重點介紹python-pptx的基本用法,注意事項,批量方法(★)。
1.2 講解清楚,注釋仔細,本人親測,注意事項提醒,適合收藏,小白秒懂。
2 python-pptx基本使用
==================
2.1 官網
https://github.com/scanny/python-pptxhttps://python-pptx.readthedocs.io/en/latest/index.html2.2 安裝
pip install python-pptx#本機安裝,國內源sudo pip3.8 install -i https://pypi.tuna.tsinghua.edu.cn/simple python-pptx2.3 首頁文字
2.3.1 代碼
#ppt首頁文字設置# 導包from pptx import Presentation# 創建空白演示文稿prs = Presentation()# 添加標題布局的幻燈片# #第0種布局是空白布局# #ppt布局,0是主標題和副標題,也就是首頁#0~6等title_slide_layout = prs.slide_layouts[0]#添加空白幻燈片slide = prs.slides.add_slide(title_slide_layout)# 設置標題和副標題#title = slide.shapes.title#等同于上面的設置title = slide.placeholders[0]#注意只能是1,不能是0,是0就等同于主標題subtitle = slide.placeholders[1]#文字內容title.text = "Hello, World!"subtitle.text = "python-pptx was here!"# 保存prs.save('/home/xgj/Desktop/python-pptx/1.pptx')2.3.1 圖
2.4 柱狀圖
2.4.1 代碼
from pptx import Presentationfrom pptx.chart.data import ChartDatafrom pptx.enum.chart import XL_CHART_TYPE#導出英尺,還可以到處Cm#from pptx.util import Inchesfrom pptx.util import Cm# 創建幻燈片 ------prs = Presentation()#推薦圖標布局5slide = prs.slides.add_slide(prs.slide_layouts[5]) # 定義圖表數據 ---------------------chart_data = ChartData()chart_data.categories = ['Dog', 'Pig', 'Cow']#數組和數據chart_data.add_series('Series 1', (29, 20, 33))chart_data.add_series('Series 2', (14, 31.4, 19.7))# 將圖表添加到幻燈片 --------------------#x和y是圖片的坐標,左頂點坐標為0,0#cx和cy是圖片的右下坐標,推薦參數數值#x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)#Cm厘米設置大小,推薦參數數值x, y, cx, cy = Cm(4), Cm(4), Cm(16), Cm(12)slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data)#圖表的標題title = slide.shapes.titletitle.text = "2組動物數據柱狀圖"prs.save('/home/xgj/Desktop/python-pptx/2-chart.pptx')2.4.2 圖
2.5 圖片加載
代碼,圖省略
from pptx import Presentationfrom pptx.util import Cmprs = Presentation()#布局6,圖片格式blank_slide_layout = prs.slide_layouts[6]#加入pptslide = prs.slides.add_slide(blank_slide_layout)#類似與x和y,代表圖片的左上角的坐標位置left = top = Cm(3)#如果不設置寬和高,就是原圖大小顯示,一般都是尺寸不合適height= Cm(12)width=Cm(10) pic = slide.shapes.add_picture('/home/xgj/Desktop/python-pptx/girl.jpeg',left,top,height=height,width=width)#原圖#pic = slide.shapes.add_picture('/home/xgj/Desktop/python-pptx/girl.jpeg',left,top)prs.save('/home/xgj/Desktop/python-pptx/3-pic.pptx')2.6 表格
2.6.1 代碼
from pptx import Presentationfrom pptx.util import Cmprs = Presentation() #使用第6個空白版式創建新的幻燈片blank_slide_layout = prs.slide_layouts[6]slide = prs.slides.add_slide(blank_slide_layout)#表格設置4行2列rows,cols = 4,2#圖表的左上角坐標left = top =Cm(5)#圖表的寬和高width = Cm(18)height = Cm(3)#表格參數導入table = slide.shapes.add_table(rows,cols,left,top,width,height).table# 可以修改列寬、行高table.columns[0].width = Cm(6)table.columns[1].width = Cm(4)table.rows[0].height =Cm(2)#數據data = [ ['姓名','成績'], #第一行,表頭設置 ['李白',99], ['王維', 92], ['杜牧', 95],]#遍歷for row in range(rows): for col in range(cols): table.cell(row,col).text =str(data[row][col])prs.save('/home/xgj/Desktop/python-pptx/4-table.pptx')2.6.2 圖
3 python-pptx高級使用
=================
3.1 組合圖
3.1.1 代碼
from pptx import Presentationfrom pptx.chart.data import ChartDatafrom pptx.enum.chart import XL_CHART_TYPEfrom pptx.util import Cm #Inchesfrom pptx.enum.chart import XL_LEGEND_POSITION# 創建幻燈片 ------prs = Presentation()title_only_slide_layout = prs.slide_layouts[5]slide = prs.slides.add_slide(title_only_slide_layout)shapes = slide.shapesshapes.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 = 4cols = 4top = Cm(12.5)left = Cm(2.5) #Inches(2.0)width = Cm(16) # 4個寬度的4height = Cm(5) # Inches(0.8)# 添加表格到幻燈片 --------------------table = shapes.add_table(rows, cols, left, top, width, height).table# 設置單元格寬度,寬度與總表格寬度的關系table.columns[0].width = Cm(4)table.columns[1].width = Cm(4)table.columns[2].width = Cm(4)table.columns[3].width = Cm(4)# 設置標題行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_objectschart_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(2.5), Cm(4.2), Cm(20), Cm(6)graphic_frame = slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data )chart = graphic_frame.chartchart.has_legend = Truechart.legend.position = XL_LEGEND_POSITION.TOPchart.legend.include_in_layout = Falsevalue_axis = chart.value_axisvalue_axis.maximum_scale = 100.0value_axis.has_title = Truevalue_axis.axis_title.has_text_frame = Truevalue_axis.axis_title.text_frame.text = "False positive"value_axis.axis_title.text_frame.auto_sizeprs.save('/home/xgj/Desktop/python-pptx/5-chart-complex.pptx')3.1.2 圖
3.2 批量生成ppt:python出來ppt的優點,舉例★
(誰用單個頁面制作ppt,不是python的優勢,批量制作才是python的優勢)
3.2.1 代碼
#批量制作ppt文件,這個比較好from pptx import Presentationfrom pptx.util import Cmimport os# 演示文稿根對象,使用默認母版prs = Presentation()# 幻燈片布局title_only_slide_layout = prs.slide_layouts[5]#名字列表,當然也可以來自文件,txt或者xls和csv等讀取提取name_list=['A','B','C','D','E','F']#讀取某路徑下的所有圖片和顯示圖片pic_dir = os.path.join("/home/xgj/Desktop/python-pptx/pic_img")#這個 文件列表安裝圖片大小排序的pic_img_names = os.listdir(pic_dir)#從新排序,按名字序號阿拉伯數字的升序pic_img_names.sort()for i in range(len(name_list)): # 1. 當前幻燈片對象 slide = prs.slides.add_slide(title_only_slide_layout) # 2. 當前shapes(背景畫布)對象 shapes = slide.shapes #標題文字 shapes.title.text = '祝'+str(name_list[i]+'同學,新年快樂!') #圖片位置和大小設置 #類似與x和y,代表圖片的左上角的坐標位置 left = top = Cm(3) #如果不設置寬和高,就是原圖大小顯示,一般都是尺寸不合適 height= Cm(12) width=Cm(10) #加載圖片到ppt每一頁去 pic = slide.shapes.add_picture(pic_dir +"/" + pic_img_names[i],left,top,height=height,width=width)#保存prs.save('/home/xgj/Desktop/python-pptx/2pil.pptx')3.2.2 圖
圖片來自網絡,僅供學習,如有侵權請聯系,定刪除。
總結
以上是生活随笔為你收集整理的poj 2480 python_python-pptx:是一个python处理ppt的库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux记住密码
- 下一篇: PPT转换成PDF后文档的背景色没有了怎