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