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
chart.font.size = Pt(12)
category_axis = chart.category_axis
category_axis.tick_labels.font.size = Pt(15)
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 = 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.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
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_pie('./办公自动化/files/ppt_pie.pptx')