小白学Python(6)——python-pptx 添加图表

2022-10-17,,,,

 

添加图表

以下代码在新演示文稿中添加单系列柱形图

 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和气泡图一直出现错误,暂时没有完成,正在研究……

《小白学Python(6)——python-pptx 添加图表.doc》

下载本文的Word格式文档,以方便收藏与打印。