亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于python-pptx庫(kù)中文文檔及使用詳解

 更新時(shí)間:2020年02月14日 09:05:44   作者:BSOD_aura  
今天小編就為大家分享一篇基于python-pptx庫(kù)中文文檔及使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

個(gè)人使用樣例及部分翻譯自官方文檔,并詳細(xì)介紹chart的使用

一:基礎(chǔ)應(yīng)用

1.創(chuàng)建pptx文檔類并插入一頁(yè)幻燈片

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

prs.slide_layouts中一共預(yù)存有1-48種,采用第六種為空白幻燈片

例slide_layouts[1]為帶標(biāo)題和正文框的ppt,slide_layouts[6]為空白頁(yè)ppt

slide 及為一頁(yè)‘幻燈片類'

修改完后 prs.save('name.pptx') 保存ppt

2.在創(chuàng)建的這頁(yè)幻燈片文本框中添加文字

body_shape = slide.shapes.placeholders # body_shape為本頁(yè)ppt中所有shapes
body_shape[0].text = 'this is placeholders[0]' # 在第一個(gè)文本框中文字框架內(nèi)添加文字
body_shape[1].text = 'this is placeholders[1]' # 在第二個(gè)文本框中文字框架內(nèi)添加文字

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

title_shape = slide.shapes.title # 取本頁(yè)ppt的title
title_shape.text = 'this is a title' # 向title文本框?qū)懭缥淖?
subtitle = slide.shapes.placeholders[1] # 取出本頁(yè)第二個(gè)文本框
subtitle.text = 'this is a subtitle' # 在第二個(gè)文本框中寫入文字

由于采用的slide_layouts[1]包含一個(gè)標(biāo)題和一個(gè)正文框,所以可以直接取slide.shapes.title 表示標(biāo)題框?qū)懭胛淖忠嗫?/p>

3.在文本框中添加新段落

from pptx.util import Pt
new_paragraph = body_shape[1].text_frame.add_paragraph() # 在第二個(gè)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 # 新段落的級(jí)別

add_paragraph中的文字支持修改font

pptx.util 中為Pt為文字大小設(shè)置

4.添加新文本框

left = top = width = height = Inches(5) # 預(yù)設(shè)位置及大小
textbox = slide.shapes.add_textbox(left, top, width, height) # left,top為相對(duì)位置,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, top, width, height = Inches(1), Inches(4.5), Inches(2), Inches(2) # 預(yù)設(shè)位置及大小
pic = slide.shapes.add_picture(img_path, left, top, width, height) # 在指定位置按預(yù)設(shè)值添加圖片

6.添加形狀

from pptx.enum.shapes import MSO_SHAPE
left, top, width, height = Inches(1), Inches(3), Inches(1.8), Inches(1) # 預(yù)設(shè)位置及大小
shape = slide.shapes.add_shape(MSO_SHAPE.PENTAGON, left, top, width, height) # 在指定位置按預(yù)設(shè)值添加類型為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, left, top, width, height)
  shape.text = 'Step{}'.format(n)

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

7.添加表格

rows, cols, left, top, width, height = 2, 2, Inches(3.5), Inches(4.5), Inches(6), Inches(0.8)
table = slide.shapes.add_table(rows, cols, left, top, width, 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

根據(jù)以上代碼生成的一頁(yè)幻燈片如下:

二: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]) # 在幻燈片中加入一頁(yè)6號(hào)風(fēng)格(空白)幻燈片
 
# chart1 左上方圖
x, y, cx, cy = Inches(0.5), Inches(0.5), Inches(4), Inches(3) # 按英尺標(biāo)準(zhǔn)指定x,y值
 
chart_data = ChartData() # 圖表data類
 
chart_data.categories = [u'A班級(jí)得分率', u'B班級(jí)得分率'] # 圖表加入兩欄
chart_data.add_series(u'得分率對(duì)比', (80.5, 60.5)) # 在兩欄分別填入數(shù)據(jù)
 
graphic_frame = slide.shapes.add_chart(
  XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
) # add_chart(圖表類型,xy表示圖表位置,cx cy表示圖表寬高,并且插入chart_data中規(guī)定好的數(shù)據(jù))
 
chart = graphic_frame.chart # 從生成的圖表中取出圖表類
chart.chart_style = 21 # 圖表整體顏色風(fēng)格
 
chart.has_title = True # 圖表是否含有標(biāo)題,默認(rèn)為False
chart.chart_title.text_frame.clear() # 清除原標(biāo)題
new_paragraph = chart.chart_title.text_frame.add_paragraph() # 添加一行新標(biāo)題
new_paragraph.text = '得分率對(duì)比' # 新標(biāo)題
new_paragraph.font.size = Pt(15) # 新標(biāo)題字體大小
 
category_axis = chart.category_axis # category_axis 為chart的category控制類
category_axis.has_major_gridlines = True # 是否顯示縱軸線
category_axis.tick_labels.font.italic = True # tick_labels為圖表下標(biāo)簽,置為斜體
category_axis.tick_labels.font.size = Pt(15) # 下標(biāo)簽字體大小
category_axis.tick_labels.font.color.rgb = RGBColor(255, 0, 0) # 標(biāo)簽字體顏色
 
value_axis = chart.value_axis # value_axis 為chart的value控制類
value_axis.maximum_scale = 100.0 # 縱坐標(biāo)最大值
value_axis.minimum_scale = 0.0 # 縱坐標(biāo)最小值
value_axis.minor_tick_mark = XL_TICK_MARK.CROSS
value_axis.has_minor_gridlines = True
 
tick_labels = value_axis.tick_labels # tick_labels 為chart的縱軸標(biāo)簽控制類
tick_labels.number_format = '0%' # 標(biāo)簽顯示樣式
tick_labels.font.bold = True # 字體加粗
tick_labels.font.size = Pt(14) # 字體大小
tick_labels.font.color.rgb = RGBColor(0, 255, 0) # 標(biāo)簽顏色
 
plot = chart.plots[0] # 取圖表中第一個(gè)plot
plot.has_data_labels = True # 是否顯示數(shù)據(jù)標(biāo)簽
data_labels = plot.data_labels # 數(shù)據(jù)標(biāo)簽控制類
data_labels.font.size = Pt(13) # 字體大小
data_labels.font.color.rgb = RGBColor(0, 0, 255) # 字體顏色
data_labels.position = XL_LABEL_POSITION.INSIDE_END # 字體位置
 
# chart 2 左下方圖
x, y, cx, cy = Inches(0.5), Inches(3.5), Inches(4), Inches(3) # 按英尺標(biāo)準(zhǔn)指定x,y值
chart_data = ChartData()
chart_data.categories = ['A', 'B', 'C', 'D']
chart_data.add_series(u'A班級(jí)選項(xiàng)占比', (80, 10, 9, 10))
chart = slide.shapes.add_chart(
  XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart # PIE為餅狀圖
 
chart.has_legend = True # 是否含有下方的說(shuō)明
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.horz_offset = 0 # 說(shuō)明位移量 [-1, 1] 默認(rèn)為0
 
chart.plots[0].has_data_labels = True # 餅中是否寫入數(shù)值
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%' # 數(shù)值顯示格式
data_labels.position = XL_LABEL_POSITION.INSIDE_END # 數(shù)值布局方式
 
chart.has_title = True
chart.chart_title.text_frame.clear() # 清除原標(biāo)題
new_paragraph = chart.chart_title.text_frame.add_paragraph() # 添加一行新標(biāo)題
new_paragraph.text = 'A班級(jí)選項(xiàng)占比' # 新標(biāo)題
new_paragraph.font.size = Pt(13) # 新標(biāo)題字體大小
 
# chart 3 右下方圖
x, y, cx, cy = Inches(5.5), Inches(4), Inches(4), Inches(3) # 按英尺標(biāo)準(zhǔn)指定x,y值
chart_data = ChartData()
chart_data.categories = ['A', 'B', 'C', 'D']
chart_data.add_series(u'B班級(jí)選項(xiàng)占比', (0.1, 0.2, 0.3, 0.4))
chart = slide.shapes.add_chart(
  XL_CHART_TYPE.PIE, x, y, cx, cy, 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() # 清除原標(biāo)題
new_paragraph = chart.chart_title.text_frame.add_paragraph() # 添加一行新標(biāo)題
new_paragraph.text = 'B班級(jí)選項(xiàng)占比' # 新標(biāo)題
new_paragraph.font.size = Pt(13) # 新標(biāo)題字體大小
 
 
# chart 4 右上方圖
x, y, cx, cy = Inches(5.5), Inches(0.5), Inches(4), 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, x, y, cx, cy, 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庫(kù)中文文檔及使用詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • pandas按行按列遍歷Dataframe的幾種方式

    pandas按行按列遍歷Dataframe的幾種方式

    這篇文章主要介紹了pandas按行按列遍歷Dataframe的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • PyQt 5 設(shè)置Logo圖標(biāo)和Title標(biāo)題的操作

    PyQt 5 設(shè)置Logo圖標(biāo)和Title標(biāo)題的操作

    這篇文章主要介紹了PyQt 5 設(shè)置Logo圖標(biāo)和Title標(biāo)題的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • 使用SAE部署Python運(yùn)行環(huán)境的教程

    使用SAE部署Python運(yùn)行環(huán)境的教程

    這篇文章主要介紹了使用SAE部署Python運(yùn)行環(huán)境的教程,SAE作為新浪的在線軟件部署平臺(tái),在國(guó)內(nèi)擁有一定的性價(jià)比,需要的朋友可以參考下
    2015-05-05
  • 關(guān)于Python形參打包與解包小技巧分享

    關(guān)于Python形參打包與解包小技巧分享

    今天小編就為大家分享一篇關(guān)于Python形參打包與解包小技巧分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • python [:3] 實(shí)現(xiàn)提取數(shù)組中的數(shù)

    python [:3] 實(shí)現(xiàn)提取數(shù)組中的數(shù)

    今天小編就為大家分享一篇python [:3] 實(shí)現(xiàn)提取數(shù)組中的數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • 對(duì)Python中實(shí)現(xiàn)兩個(gè)數(shù)的值交換的集中方法詳解

    對(duì)Python中實(shí)現(xiàn)兩個(gè)數(shù)的值交換的集中方法詳解

    今天小編就為大家分享一篇對(duì)Python中實(shí)現(xiàn)兩個(gè)數(shù)的值交換的集中方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Django實(shí)現(xiàn)上傳圖片功能

    Django實(shí)現(xiàn)上傳圖片功能

    這篇文章為大家詳細(xì)主要介紹了Django實(shí)現(xiàn)上傳圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Python操作MySQL數(shù)據(jù)庫(kù)的三種方法總結(jié)

    Python操作MySQL數(shù)據(jù)庫(kù)的三種方法總結(jié)

    下面小編就為大家分享一篇Python操作MySQL數(shù)據(jù)庫(kù)的三種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • jupyter notebook 多環(huán)境conda kernel配置方式

    jupyter notebook 多環(huán)境conda kernel配置方式

    這篇文章主要介紹了jupyter notebook 多環(huán)境conda kernel配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Python內(nèi)存管理實(shí)例分析

    Python內(nèi)存管理實(shí)例分析

    這篇文章主要介紹了Python內(nèi)存管理,結(jié)合實(shí)例形式分析了Python對(duì)象的引用、垃圾回收、分配等相關(guān)原理與操作技巧,需要的朋友可以參考下
    2019-07-07

最新評(píng)論