1. 程式人生 > 程式設計 >基於python-pptx庫中文文件及使用詳解

基於python-pptx庫中文文件及使用詳解

個人使用樣例及部分翻譯自官方文件,並詳細介紹chart的使用

一:基礎應用

1.建立pptx文件類並插入一頁幻燈片

from pptx import Presentation
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[1])
# 對ppt的修改
prs.save('python-pptx.pptx')

prs.slide_layouts中一共預存有1-48種,採用第六種為空白幻燈片

例slide_layouts[1]為帶標題和正文框的ppt,slide_layouts[6]為空白頁ppt

slide 及為一頁‘幻燈片類'

修改完後 prs.save('name.pptx') 儲存ppt

2.在建立的這頁幻燈片文字框中新增文字

body_shape = slide.shapes.placeholders # body_shape為本頁ppt中所有shapes
body_shape[0].text = 'this is placeholders[0]' # 在第一個文字框中文字框架內新增文字
body_shape[1].text = 'this is placeholders[1]' # 在第二個文字框中文字框架內新增文字

在ppt中所有的元素均被當成一個shape,slide.shapes表示幻燈片類中的模型類,placeholders中為每個模型,採用slide_layouts[1]中包含兩個文字框,所以print len(slide.shapes.placeholders) 話為 2。

title_shape = slide.shapes.title # 取本頁ppt的title
title_shape.text = 'this is a title' # 向title文字框寫如文字
subtitle = slide.shapes.placeholders[1] # 取出本頁第二個文字框
subtitle.text = 'this is a subtitle' # 在第二個文字框中寫入文字

由於採用的slide_layouts[1]包含一個標題和一個正文框,所以可以直接取slide.shapes.title 表示標題框寫入文字亦可

3.在文字框中新增新段落

from pptx.util import Pt
new_paragraph = body_shape[1].text_frame.add_paragraph() # 在第二個shape中的文字框架中新增新段落
new_paragraph.text = 'add_paragraph' # 新段落中文字
new_paragraph.font.bold = True # 文字加粗
new_paragraph.font.italic = True # 文字斜體
new_paragraph.font.size = Pt(15) # 文字大小
new_paragraph.font.underline = True # 文字下劃線
new_paragraph.level = 1 # 新段落的級別

add_paragraph中的文字支援修改font

pptx.util 中為Pt為文字大小設定

4.新增新文字框

left = top = width = height = Inches(5) # 預設位置及大小
textbox = slide.shapes.add_textbox(left,top,width,height) # left,top為相對位置,width,height為文字框大小
textbox.text = 'this is a new textbox' # 文字框中文字
new_para = textbox.text_frame.add_paragraph() # 在新文字框中新增段落
new_para.text = 'this is second para in textbox' # 段落文字

5.新增圖片

img_path = 'img_path.jpg' # 檔案路徑
left,height = Inches(1),Inches(4.5),Inches(2),Inches(2) # 預設位置及大小
pic = slide.shapes.add_picture(img_path,left,height) # 在指定位置按預設值新增圖片

6.新增形狀

from pptx.enum.shapes import MSO_SHAPE
left,Inches(3),Inches(1.8),Inches(1) # 預設位置及大小
shape = slide.shapes.add_shape(MSO_SHAPE.PENTAGON,height) # 在指定位置按預設值新增型別為PENTAGON的形狀
shape.text = 'Step 1'
for n in range(2,6):
  left = left + width - Inches(0.3)
  shape = slide.shapes.add_shape(MSO_SHAPE.CHEVRON,height)
  shape.text = 'Step{}'.format(n)

MSO_SHAPE中有office中各型別形狀,詳見:https://msdn.microsoft.com/en-us/library/office/ff862770(v=office.15).aspx

7.新增表格

rows,cols,height = 2,2,Inches(3.5),Inches(6),Inches(0.8)
table = slide.shapes.add_table(rows,height).table # 新增表格,並取表格類
table.columns[0].width = Inches(2.0) # 第一縱列寬度
table.columns[1].width = Inches(4.0) # 第二縱列寬度
table.cell(0,0).text = 'text00' # 指定位置寫入文字
table.cell(0,1).text = 'text01'
table.cell(1,0).text = 'text10'
table.cell(1,1).text = 'text11'

8.demo

根據以上程式碼生成的一頁幻燈片如下:

基於python-pptx庫中文文件及使用詳解

二:chart類

#!/usr/bin/env python
# encoding: utf-8
 
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
from pptx.enum.chart import XL_TICK_MARK
from pptx.util import Pt
from pptx.dml.color import RGBColor
from pptx.enum.chart import XL_LABEL_POSITION
from pptx.enum.chart import XL_LEGEND_POSITION
 
prs = Presentation()
 
slide = prs.slides.add_slide(prs.slide_layouts[6]) # 在幻燈片中加入一頁6號風格(空白)幻燈片
 
# chart1 左上方圖
x,y,cx,cy = Inches(0.5),Inches(0.5),Inches(4),Inches(3) # 按英尺標準指定x,y值
 
chart_data = ChartData() # 圖表data類
 
chart_data.categories = [u'A班級得分率',u'B班級得分率'] # 圖表加入兩欄
chart_data.add_series(u'得分率對比',(80.5,60.5)) # 在兩欄分別填入資料
 
graphic_frame = slide.shapes.add_chart(
  XL_CHART_TYPE.COLUMN_CLUSTERED,x,cy,chart_data
) # add_chart(圖表型別,xy表示圖表位置,cx cy表示圖表寬高,並且插入chart_data中規定好的資料)
 
chart = graphic_frame.chart # 從生成的圖表中取出圖表類
chart.chart_style = 21 # 圖表整體顏色風格
 
chart.has_title = True # 圖表是否含有標題,預設為False
chart.chart_title.text_frame.clear() # 清除原標題
new_paragraph = chart.chart_title.text_frame.add_paragraph() # 新增一行新標題
new_paragraph.text = '得分率對比' # 新標題
new_paragraph.font.size = Pt(15) # 新標題字型大小
 
category_axis = chart.category_axis # category_axis 為chart的category控制類
category_axis.has_major_gridlines = True # 是否顯示縱軸線
category_axis.tick_labels.font.italic = True # tick_labels為圖表下標籤,置為斜體
category_axis.tick_labels.font.size = Pt(15) # 下標籤字型大小
category_axis.tick_labels.font.color.rgb = RGBColor(255,0) # 標籤字型顏色
 
value_axis = chart.value_axis # value_axis 為chart的value控制類
value_axis.maximum_scale = 100.0 # 縱座標最大值
value_axis.minimum_scale = 0.0 # 縱座標最小值
value_axis.minor_tick_mark = XL_TICK_MARK.CROSS
value_axis.has_minor_gridlines = True
 
tick_labels = value_axis.tick_labels # tick_labels 為chart的縱軸標籤控制類
tick_labels.number_format = '0%' # 標籤顯示樣式
tick_labels.font.bold = True # 字型加粗
tick_labels.font.size = Pt(14) # 字型大小
tick_labels.font.color.rgb = RGBColor(0,255,0) # 標籤顏色
 
plot = chart.plots[0] # 取圖表中第一個plot
plot.has_data_labels = True # 是否顯示資料標籤
data_labels = plot.data_labels # 資料標籤控制類
data_labels.font.size = Pt(13) # 字型大小
data_labels.font.color.rgb = RGBColor(0,255) # 字型顏色
data_labels.position = XL_LABEL_POSITION.INSIDE_END # 字型位置
 
# chart 2 左下方圖
x,Inches(3) # 按英尺標準指定x,y值
chart_data = ChartData()
chart_data.categories = ['A','B','C','D']
chart_data.add_series(u'A班級選項佔比',(80,10,9,10))
chart = slide.shapes.add_chart(
  XL_CHART_TYPE.PIE,chart_data
).chart # PIE為餅狀圖
 
chart.has_legend = True # 是否含有下方的說明
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.horz_offset = 0 # 說明位移量 [-1,1] 預設為0
 
chart.plots[0].has_data_labels = True # 餅中是否寫入數值
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%' # 數值顯示格式
data_labels.position = XL_LABEL_POSITION.INSIDE_END # 數值佈局方式
 
chart.has_title = True
chart.chart_title.text_frame.clear() # 清除原標題
new_paragraph = chart.chart_title.text_frame.add_paragraph() # 新增一行新標題
new_paragraph.text = 'A班級選項佔比' # 新標題
new_paragraph.font.size = Pt(13) # 新標題字型大小
 
# chart 3 右下方圖
x,cy = Inches(5.5),'D']
chart_data.add_series(u'B班級選項佔比',(0.1,0.2,0.3,0.4))
chart = slide.shapes.add_chart(
  XL_CHART_TYPE.PIE,chart_data
).chart
 
chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
 
chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'
data_labels.position = XL_LABEL_POSITION.INSIDE_END
 
chart.has_title = True
chart.chart_title.text_frame.clear() # 清除原標題
new_paragraph = chart.chart_title.text_frame.add_paragraph() # 新增一行新標題
new_paragraph.text = 'B班級選項佔比' # 新標題
new_paragraph.font.size = Pt(13) # 新標題字型大小
 
 
# chart 4 右上方圖
x,Inches(3)
chart_data = ChartData()
chart_data.categories = ['0','1-3','4-6','7-9']
chart_data.add_series('',(50,18,30,34))
chart = slide.shapes.add_chart(
  XL_CHART_TYPE.PIE,chart_data
).chart
 
chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.font.size = Pt(13)
 
chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'
data_labels.position = XL_LABEL_POSITION.INSIDE_END
 
chart.has_title = True
chart.chart_title.text_frame.clear()
new_title = chart.chart_title.text_frame.add_paragraph()
new_title.text = '得分佔比'
new_title.font.size = Pt(13)
 
prs.save('test.pptx')

生成demo:

基於python-pptx庫中文文件及使用詳解

以上這篇基於python-pptx庫中文文件及使用詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。