Python報(bào)表系列_PPTX

2019年7月3日10:49:10


    
    (|3[▓▓] 晚安

python 在 word ppt pdf 生成折線(xiàn)圖、柱狀圖、餅狀圖,這里是ppt的操作。

環(huán)境

python 3.6
python-pptx (pip install python-pptx) 

使用說(shuō)明

1、新建demo_pptx.py
2、復(fù)制以下代碼到py文件中
3、python demo_pptx.py 運(yùn)行即可生成pptx文件

demo_pptx.py代碼如下:

from pptx import Presentation
from pptx.chart.data import CategoryChartData, ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches, Cm
from pptx.enum.chart import XL_TICK_MARK
from pptx.util import Pt
from pptx.dml.color import RGBColor

prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])

# 柱狀圖
chart_data = ChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Q1 Sales', (19.2, 21.4, 16.7))
chart_data.add_series('Q2 Sales', (22.3, 28.6, 15.2))
chart_data.add_series('Q3 Sales', (20.4, 26.3, 14.2))

x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(2)
graphic_frame = slide.shapes.add_chart(
    XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
)

chart = graphic_frame.chart

# 添加第二頁(yè) 折線(xiàn)圖
slide = prs.slides.add_slide(prs.slide_layouts[5])
chart_data = ChartData()
chart_data.categories = ['Q1 Sales', 'Q2 Sales', 'Q3 Sales']
chart_data.add_series('West', (32.2, 28.4, 34.7))
chart_data.add_series('East', (24.3, 30.6, 20.2))
chart_data.add_series('Midwest', (20.4, 18.3, 26.2))

x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(2)
chart = slide.shapes.add_chart(
    XL_CHART_TYPE.LINE, x, y, cx, cy, chart_data
).chart

chart.has_legend = True
chart.legend.include_in_layout = False
chart.series[0].smooth = True

# 添加第三頁(yè) 表格
slide = prs.slides.add_slide(prs.slide_layouts[5])
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(0.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])

prs.save('demo.pptx')

輸出如下

demo

參考文獻(xiàn):

    官方文檔:https://python-pptx.readthedocs.io/en/latest/user/charts.html

有什么問(wèn)題歡迎隨時(shí)討論

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、Python簡(jiǎn)介和環(huán)境搭建以及pip的安裝 4課時(shí)實(shí)驗(yàn)課主要內(nèi)容 【Python簡(jiǎn)介】: Python 是一個(gè)...
    _小老虎_閱讀 6,340評(píng)論 0 10
  • 米斯特鄒閱讀 205評(píng)論 0 1
  • 我趴在床上,牙疼了一晚,循環(huán)播放著張信哲的《難以抗拒你容顏》,望著窗外淅淅瀝瀝的小雨,莫名的懷念起一個(gè)叫麗江的姑娘...
    芳芳樂(lè)分享閱讀 397評(píng)論 3 2
  • 哎想了半天題目,今天就隨便寫(xiě)寫(xiě)吧,想到哪里寫(xiě)到哪。 其實(shí)生活中有很多時(shí)候都想說(shuō)點(diǎn)什么,但好像真的要提起筆寫(xiě)下來(lái),靈...
    藍(lán)小小小小魚(yú)閱讀 283評(píng)論 0 0
  • 郴州市首屆小籃球比賽開(kāi)始啦。你們是不是很期待呀,對(duì)!我很期待。 郭教練帶我們來(lái)到籃球場(chǎng),讓我們熟悉籃筐。沒(méi)多久,外...
    無(wú)敵小品子閱讀 743評(píng)論 0 7

友情鏈接更多精彩內(nèi)容