1 from pptx import Presentation 2 from pptx.util import Inches 3 from pptx.chart.data import ChartData 4 from pptx.enum.chart import XL_TICK_MARK 5 from pptx.util import Pt 6 from pptx.dml.color import RGBColor 7 from pptx.enum.chart import XL_DATA_LABEL_POSITION 8 from pptx.enum.chart import XL_LEGEND_POSITION 9 from pptx.enum.chart import XL_CHART_TYPE 10 from pptx.enum.chart import XL_MARKER_STYLE 11 12 prs = Presentation() 13 14 slide = prs.slides.add_slide(prs.slide_layouts[6]) # 在幻灯片中加入一页6号风格(空白)幻灯片 15 16 # chart1 左上方图 17 x, y, cx, cy = Inches(0.5), Inches(0.5), Inches(4), Inches(3) # 按英尺标准指定x,y值 18 19 chart_data = ChartData() # 图表data类 20 21 chart_data.categories = [u'A班级得分率', u'B班级得分率'] # 图表加入两栏 22 chart_data.add_series(u'得分率对比', (80.5, 60.5)) # 在两栏分别填入数据 23 24 graphic_frame = slide.shapes.add_chart( 25 XL_CHART_TYPE.BAR_CLUSTERED, x, y, cx, cy, chart_data) 26 # add_chart(图表类型,xy表示图表位置,cx cy表示图表宽高,并且插入chart_data中规定好的数据) 27 28 chart = graphic_frame.chart # 从生成的图表中取出图表类 29 chart.chart_style = 10 # 图表整体颜色风格 30 31 chart.has_title = True # 图表是否含有标题,默认为False 32 chart.chart_title.text_frame.clear() # 清除原标题 33 new_paragraph = chart.chart_title.text_frame.add_paragraph() # 添加一行新标题 34 new_paragraph.text = '得分率对比' # 新标题 35 new_paragraph.font.size = Pt(15) # 新标题字体大小 36 37 category_axis = chart.category_axis # category_axis 为chart的category控制类 38 category_axis.has_major_gridlines = True # 是否显示纵轴线 39 category_axis.tick_labels.font.italic = True # tick_labels为图表下标签,置为斜体 40 category_axis.tick_labels.font.size = Pt(15) # 下标签字体大小 41 category_axis.tick_labels.font.color.rgb = RGBColor(255, 0, 0) # 标签字体颜色 42 43 value_axis = chart.value_axis # value_axis 为chart的value控制类 44 value_axis.maximum_scale = 100.0 # 纵坐标最大值 45 value_axis.minimum_scale = 0.0 # 纵坐标最小值 46 value_axis.minor_tick_mark = XL_TICK_MARK.CROSS 47 value_axis.has_minor_gridlines = True 48 49 tick_labels = value_axis.tick_labels # tick_labels 为chart的纵轴标签控制类 50 tick_labels.number_format = '0%' # 标签显示样式 51 tick_labels.font.bold = True # 字体加粗 52 tick_labels.font.size = Pt(14) # 字体大小 53 tick_labels.font.color.rgb = RGBColor(0, 255, 0) # 标签颜色 54 55 plot = chart.plots[0] # 取图表中第一个plot 56 plot.has_data_labels = True # 是否显示数据标签 57 data_labels = plot.data_labels # 数据标签控制类 58 data_labels.font.size = Pt(13) # 字体大小 59 data_labels.font.color.rgb = RGBColor(0, 0, 255) # 字体颜色 60 data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END # 字体位置 61 62 # chart 2 左下方图 63 x, y, cx, cy = Inches(0.5), Inches(3.5), Inches(4), Inches(3) # 按英尺标准指定x,y值 64 chart_data = ChartData() 65 chart_data.categories = ['A', 'B', 'C', 'D'] 66 chart_data.add_series(u'A班级选项占比', (80, 10, 9, 10)) 67 chart = slide.shapes.add_chart( 68 XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data 69 ).chart # PIE为饼状图 70 chart.has_legend = True # 是否含有下方的说明 71 chart.legend.position = XL_LEGEND_POSITION.BOTTOM 72 chart.legend.horz_offset = 0 # 说明位移量 [-1, 1] 默认为0 73 chart.plots[0].has_data_labels = True # 饼中是否写入数值 74 data_labels = chart.plots[0].data_labels 75 data_labels.number_format = '0%' # 数值显示格式 76 data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END # 数值布局方式 77 78 chart.has_title = True 79 chart.chart_title.text_frame.clear() # 清除原标题 80 new_paragraph = chart.chart_title.text_frame.add_paragraph() # 添加一行新标题 81 new_paragraph.text = 'A班级选项占比' # 新标题 82 new_paragraph.font.size = Pt(13) # 新标题字体大小 83 84 # chart 3 右下方图 85 x, y, cx, cy = Inches(5.5), Inches(4), Inches(4), Inches(3) # 按英尺标准指定x,y值 86 chart_data = ChartData() 87 chart_data.categories = ['A', 'B', 'C', 'D'] 88 chart_data.add_series(u'B班级选项占比', (0.1, 0.2, 0.3, 0.4)) 89 chart = slide.shapes.add_chart( 90 XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data 91 ).chart 92 93 chart.has_legend = True 94 chart.legend.position = XL_LEGEND_POSITION.BOTTOM 95 96 chart.plots[0].has_data_labels = True 97 data_labels = chart.plots[0].data_labels 98 data_labels.number_format = '0%' 99 data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END 100 chart.has_title = True 101 chart.chart_title.text_frame.clear() # 清除原标题 102 new_paragraph = chart.chart_title.text_frame.add_paragraph() # 添加一行新标题 103 new_paragraph.text = 'B班级选项占比' # 新标题 104 new_paragraph.font.size = Pt(13) # 新标题字体大小 105 106 107 # chart 4 右上方图 108 x, y, cx, cy = Inches(5.5), Inches(0.5), Inches(4), Inches(3) 109 chart_data = ChartData() 110 chart_data.categories = ['0', '1-3', '4-6', '7-9'] 111 chart_data.add_series('', (50, 18, 30, 34)) 112 chart = slide.shapes.add_chart( 113 XL_CHART_TYPE.PIE_EXPLODED, x, y, cx, cy, chart_data 114 ).chart 115 116 chart.has_legend = True 117 chart.legend.position = XL_LEGEND_POSITION.BOTTOM 118 chart.legend.font.size = Pt(13) 119 chart.plots[0].has_data_labels = True 120 data_labels = chart.plots[0].data_labels 121 data_labels.number_format = '0%' 122 data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END 123 124 chart.has_title = True 125 chart.chart_title.text_frame.clear() 126 new_title = chart.chart_title.text_frame.add_paragraph() 127 new_title.text = '得分占比' 128 new_title.font.size = Pt(13) 129 130 prs.save('test.pptx')
标签:pptx,XL,Inches,python,True,labels,chart,学习,data From: https://www.cnblogs.com/dockers/p/18139833