小白學Python(6)——python-pptx 添加圖表


 

添加圖表

以下代碼在新演示文稿中添加單系列柱形圖

 1 from pptx import Presentation
 2 from pptx.chart.data import CategoryChartData
 3 from pptx.enum.chart import XL_CHART_TYPE
 4 from pptx.util import Inches
 5 
 6 # create presentation with 1 slide ------
 7 prs = Presentation()
 8 slide = prs.slides.add_slide(prs.slide_layouts[5])
 9 
10 # define chart data ---------------------
11 chart_data = CategoryChartData()
12 chart_data.categories = ['East', 'West', 'Midwest']
13 chart_data.add_series('Series 1', (19.2, 21.4, 16.7))
14 
15 # add chart to slide --------------------
16 x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
17 slide.shapes.add_chart(
18     XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
19 )
20 
21 prs.save('chart-01.pptx')

 

 1 from pptx import Presentation
 2 from pptx.chart.data import CategoryChartData
 3 from pptx.enum.chart import XL_CHART_TYPE
 4 from pptx.util import Inches
 5 
 6 # create presentation with 1 slide ------
 7 prs = Presentation()
 8 slide = prs.slides.add_slide(prs.slide_layouts[5])
 9 
10 # define chart data ---------------------
11 chart_data = CategoryChartData()
12 chart_data.categories = ['East', 'West', 'Midwest']
13 chart_data.add_series('Q1 Sales', (19.2, 21.4, 16.7))
14 chart_data.add_series('Q2 Sales', (22.3, 28.6, 15.2))
15 chart_data.add_series('Q3 Sales', (20.4, 26.3, 14.2))
16 
17 
18 # add chart to slide --------------------
19 x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
20 slide.shapes.add_chart(
21     XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
22 )
23 
24 graphic_frame = slide.shapes.add_chart(
25     XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
26 )
27 
28 chart = graphic_frame.chart
29 
30 prs.save('chart-01.pptx')

請注意,我們捕獲了add_chart()調用返回的形狀引用 graphic_frame,然后使用其chart屬性從圖形框架中提取圖表對象 我們需要圖表參考來獲取我們在接下來的步驟中需要的屬性。該 add_chart()方法不直接返回圖表對象。那是因為圖表本身並不是一種形狀。相反,它是圖形框架形狀中包含的圖形(DrawingML)對象表也​​以這種方式工作,也包含在圖形框架形狀中。

 

 1 from pptx import Presentation
 2 from pptx.chart.data import CategoryChartData
 3 from pptx.enum.chart import XL_CHART_TYPE
 4 from pptx.util import Inches
 5 
 6 # create presentation with 1 slide ------
 7 prs = Presentation()
 8 slide = prs.slides.add_slide(prs.slide_layouts[5])
 9 
10 # define chart data ---------------------
11 chart_data = CategoryChartData()
12 chart_data.categories = ['East', 'West', 'Midwest']
13 chart_data.add_series('Q1 Sales', (19.2, 21.4, 16.7))
14 chart_data.add_series('Q2 Sales', (22.3, 28.6, 15.2))
15 chart_data.add_series('Q3 Sales', (20.4, 26.3, 14.2))
16 
17 
18 # add chart to slide --------------------
19 
20 x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
21 
22 slide.shapes.add_chart(
23     XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
24 )
25 
26 graphic_frame = slide.shapes.add_chart(
27     XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
28 )
29 
30 chart = graphic_frame.chart
31 
32 from pptx.dml.color import RGBColor
33 from pptx.enum.chart import XL_LABEL_POSITION
34 
35 plot = chart.plots[0]
36 plot.has_data_labels = True
37 data_labels = plot.data_labels
38 
39 
40 data_labels.font.color.rgb = RGBColor(0x0A, 0x42, 0x80)
41 data_labels.position = XL_LABEL_POSITION.INSIDE_END
42 
43 prs.save('chart-01.pptx')

后面加上

1 from pptx.enum.chart import XL_LEGEND_POSITION
2 
3 chart.has_legend = True
4 chart.legend.position = XL_LEGEND_POSITION.RIGHT
5 chart.legend.include_in_layout = False

 1 from pptx import Presentation
 2 from pptx.chart.data import CategoryChartData
 3 from pptx.enum.chart import XL_CHART_TYPE
 4 from pptx.util import Inches
 5 
 6 # create presentation with 1 slide ------
 7 prs = Presentation()
 8 slide = prs.slides.add_slide(prs.slide_layouts[5])
 9 
10 # define chart data ---------------------
11 chart_data = CategoryChartData()
12 
13 chart_data.categories = ['Q1 Sales', 'Q2 Sales', 'Q3 Sales']
14 chart_data.add_series('West',    (32.2, 28.4, 34.7))
15 chart_data.add_series('East',    (24.3, 30.6, 20.2))
16 chart_data.add_series('Midwest', (20.4, 18.3, 26.2))
17 
18 x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
19 
20 chart = slide.shapes.add_chart(
21     XL_CHART_TYPE.LINE, x, y, cx, cy, chart_data
22 ).chart
23 
24 prs.save('chart-01.pptx')

 

 

折線圖的方式與條形圖或柱形圖幾乎相同,主要區別在於add_chart()調用中提供的圖表類型。

 

XY和氣泡圖一直出現錯誤,暫時沒有完成,正在研究……


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM