5.2 ppt 绘制bar、line、pie图表

def use_bar(path):
    from pptx import Presentation
    from pptx.chart.data import CategoryChartData
    from pptx.enum.chart import XL_CHART_TYPE
    from pptx.util import Inches

    ppt = Presentation()
    slide = ppt.slides.add_slide(ppt.slide_layouts[6])

    # 封装图表数据
    chart_data = CategoryChartData()

    # 分组数据
    chart_data.categories = ['第一季度', '第二季度', '第三季度', '第四季度',]

    # 具体数据
    chart_data.add_series('series', (19, 21, 16, 30))

    # 绘制图表
    X = Inches(2)
    Y = Inches(2)
    width = Inches(6)
    height = Inches(3)
    slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED,
                           X, Y, width, height, chart_data)

    ppt.save(path)

def use_bar_more(path):
    from pptx import Presentation
    from pptx.chart.data import CategoryChartData
    from pptx.enum.chart import XL_CHART_TYPE, XL_DATA_LABEL_POSITION, XL_LEGEND_POSITION
    from pptx.util import Inches, Pt

    ppt = Presentation()
    slide = ppt.slides.add_slide(ppt.slide_layouts[6])

    # 封装图表数据
    chart_data = CategoryChartData()

    # 分组数据
    chart_data.categories = ['第一季度', '第二季度', '第三季度', '第四季度',]

    # 具体数据
    chart_data.add_series('series1', (19, 21, 16, 30))
    chart_data.add_series('series2', (33, 16, 26, 28))
    chart_data.add_series('series3', (26, 18, 37, 20))

    # 绘制图表
    X = Inches(2)
    Y = Inches(2)
    width = Inches(6)
    height = Inches(3)
    chart = slide.shapes.add_chart(
        XL_CHART_TYPE.COLUMN_CLUSTERED, X, Y, width, height, chart_data).chart
    chart.chart_style = 10  # 1-48 主题颜色
    chart.font.size = Pt(12)

    category_axis = chart.category_axis
    category_axis.tick_labels.font.size = Pt(15)  # 设置axis的字体大小
    category_axis.has_major_gridlines = True  # 背景是否显示为表格

    # 设置标签
    plot = chart.plots[0]
    plot.has_data_labels = True  # 显示标签的值
    data_labels = plot.data_labels
    data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END

    # 设置图例
    chart.has_legend = True
    chart.legend.font.size = Pt(15)
    chart.legend.position = XL_LEGEND_POSITION.BOTTOM   # 图例放置底部
    chart.legend.include_in_layout = False    # 防止数据重叠

    ppt.save(path)

def use_line_more(path):
    from pptx import Presentation
    from pptx.util import Pt, Cm
    from pptx.chart.data import ChartData
    from pptx.enum.chart import XL_CHART_TYPE

    # 设置需要添加到哪一页
    n_page = 0

    # 打开已存在ppt
    # ppt = Presentation('python-pptx操作模板.pptx')

    # 获取slide对象
    # slide = ppt.slides[n_page]

    ppt = Presentation()
    slide = ppt.slides.add_slide(ppt.slide_layouts[6])

    # 初始化图表
    chart_data = ChartData()

    # 填充需要添加的内容
    content_arr = [["4/30-5/14", "DVT1", "20", "12", "22", "25", "5"],
                   ["5/15-5/21", "DVT1", "25", "32", "42", "30", "8"],
                   ["5/22-6/28", "DVT1", "1", "27", "37", "56", "12"],
                   ["5/22-6/28", "DVT1", "1", "27", "37", "56", "12"]]

    # 填充图表
    chart_data.categories = [
        content_arr[0][0], content_arr[1][0], content_arr[2][0], content_arr[3][0]]
    chart_data.add_series(
        "问题总数", (content_arr[0][4], content_arr[1][4], content_arr[2][4], content_arr[3][4]))
    chart_data.add_series(
        "遗留问题总数", (content_arr[0][5], content_arr[1][5], content_arr[2][5], content_arr[3][5]))
    chart_data.add_series(
        "遗留致命严重\n问题总数", (content_arr[0][6], content_arr[1][6], content_arr[2][6], content_arr[3][6]))

    # 设置位置
    left, top, width, height = Cm(6), Cm(10), Cm(16.1), Cm(7.5)
    # 添加图表
    chart = slide.shapes.add_chart(
        XL_CHART_TYPE.LINE, left, top, width, height, chart_data
    ).chart

    chart.has_legend = True
    chart.legend.include_in_layout = False
    # chart.series[0].smooth = True # 是否平滑
    # chart.series[1].smooth = True
    # chart.series[2].smooth = True
    chart.font.size = Pt(10)  # 文字大小

    ppt.save(path)

# 饼图
def use_pie(path):
    from pptx import Presentation
    from pptx.chart.data import CategoryChartData
    from pptx.enum.chart import XL_CHART_TYPE, XL_DATA_LABEL_POSITION, XL_LEGEND_POSITION
    from pptx.util import Inches, Pt

    ppt = Presentation()
    slide = ppt.slides.add_slide(ppt.slide_layouts[6])

    # 封装图表数据
    chart_data = CategoryChartData()

    # 分组数据
    chart_data.categories = ['第一季度', '第二季度', '第三季度', '第四季度',]

    # 具体数据
    chart_data.add_series('销售比例', (0.27, 0.23, 0.31, 0.19))

    # 绘制图表
    X = Inches(1)
    Y = Inches(1)
    width = Inches(5)
    height = Inches(5)
    chart = slide.shapes.add_chart(
        XL_CHART_TYPE.PIE, X, Y, width, height, chart_data).chart
    chart.chart_style = 10  # 1-48 主题颜色
    chart.font.size = Pt(12)

    # 设置标签
    plot = chart.plots[0]
    plot.has_data_labels = True  # 显示标签的值
    data_labels = plot.data_labels
    data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END
    data_labels.number_format = '0%'   # 显示百分比

    # 设置图例
    chart.has_legend = True
    chart.legend.font.size = Pt(15)
    chart.legend.position = XL_LEGEND_POSITION.BOTTOM   # 图例放置底部
    chart.legend.include_in_layout = False    # 防止数据重叠

    ppt.save(path)

    ppt.save(path)

if __name__ == '__main__':
    # use_bar('./办公自动化/files/ppt_bar.pptx')
    # use_bar_more('./办公自动化/files/ppt_bar.pptx')
    # use_line_more('./办公自动化/files/ppt_line.pptx')
    use_pie('./办公自动化/files/ppt_pie.pptx')