python -pptx


from pptx import Presentation
from pptx.util import Inches
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_TICK_MARK
from pptx.util import Pt
from pptx.dml.color import RGBColor
from pptx.enum.chart import XL_DATA_LABEL_POSITION
from pptx.enum.chart import XL_LEGEND_POSITION
from pptx.enum.chart import XL_CHART_TYPE
from pptx.enum.chart import XL_MARKER_STYLE

prs = Presentation()

slide = prs.slides.add_slide(prs.slide_layouts[6]) # 在幻燈片中加入一頁6號風格(空白)幻燈片

# chart1 左上方圖
x, y, cx, cy = Inches(0.5), Inches(0.5), Inches(4), Inches(3) # 按英尺標准指定x,y值

chart_data = ChartData() # 圖表data類

chart_data.categories = [u'A班級得分率', u'B班級得分率'] # 圖表加入兩欄
chart_data.add_series(u'得分率對比', (80.5, 60.5)) # 在兩欄分別填入數據

graphic_frame = slide.shapes.add_chart(
XL_CHART_TYPE.BAR_CLUSTERED, x, y, cx, cy, chart_data)
# add_chart(圖表類型,xy表示圖表位置,cx cy表示圖表寬高,並且插入chart_data中規定好的數據)

chart = graphic_frame.chart # 從生成的圖表中取出圖表類
chart.chart_style = 10 # 圖表整體顏色風格

chart.has_title = True # 圖表是否含有標題,默認為False
chart.chart_title.text_frame.clear() # 清除原標題
new_paragraph = chart.chart_title.text_frame.add_paragraph() # 添加一行新標題
new_paragraph.text = '得分率對比' # 新標題
new_paragraph.font.size = Pt(15) # 新標題字體大小

category_axis = chart.category_axis # category_axis 為chart的category控制類
category_axis.has_major_gridlines = True # 是否顯示縱軸線
category_axis.tick_labels.font.italic = True # tick_labels為圖表下標簽,置為斜體
category_axis.tick_labels.font.size = Pt(15) # 下標簽字體大小
category_axis.tick_labels.font.color.rgb = RGBColor(255, 0, 0) # 標簽字體顏色

value_axis = chart.value_axis # value_axis 為chart的value控制類
value_axis.maximum_scale = 100.0 # 縱坐標最大值
value_axis.minimum_scale = 0.0 # 縱坐標最小值
value_axis.minor_tick_mark = XL_TICK_MARK.CROSS
value_axis.has_minor_gridlines = True

tick_labels = value_axis.tick_labels # tick_labels 為chart的縱軸標簽控制類
tick_labels.number_format = '0%' # 標簽顯示樣式
tick_labels.font.bold = True # 字體加粗
tick_labels.font.size = Pt(14) # 字體大小
tick_labels.font.color.rgb = RGBColor(0, 255, 0) # 標簽顏色

plot = chart.plots[0] # 取圖表中第一個plot
plot.has_data_labels = True # 是否顯示數據標簽
data_labels = plot.data_labels # 數據標簽控制類
data_labels.font.size = Pt(13) # 字體大小
data_labels.font.color.rgb = RGBColor(0, 0, 255) # 字體顏色
data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END # 字體位置

# chart 2 左下方圖
x, y, cx, cy = Inches(0.5), Inches(3.5), Inches(4), Inches(3) # 按英尺標准指定x,y值
chart_data = ChartData()
chart_data.categories = ['A', 'B', 'C', 'D']
chart_data.add_series(u'A班級選項占比', (80, 10, 9, 10))
chart = slide.shapes.add_chart(
XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart # PIE為餅狀圖
chart.has_legend = True # 是否含有下方的說明
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.horz_offset = 0 # 說明位移量 [-1, 1] 默認為0
chart.plots[0].has_data_labels = True # 餅中是否寫入數值
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%' # 數值顯示格式
data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END # 數值布局方式

chart.has_title = True
chart.chart_title.text_frame.clear() # 清除原標題
new_paragraph = chart.chart_title.text_frame.add_paragraph() # 添加一行新標題
new_paragraph.text = 'A班級選項占比' # 新標題
new_paragraph.font.size = Pt(13) # 新標題字體大小

# chart 3 右下方圖
x, y, cx, cy = Inches(5.5), Inches(4), Inches(4), Inches(3) # 按英尺標准指定x,y值
chart_data = ChartData()
chart_data.categories = ['A', 'B', 'C', 'D']
chart_data.add_series(u'B班級選項占比', (0.1, 0.2, 0.3, 0.4))
chart = slide.shapes.add_chart(
XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart

chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM

chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'
data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END
chart.has_title = True
chart.chart_title.text_frame.clear() # 清除原標題
new_paragraph = chart.chart_title.text_frame.add_paragraph() # 添加一行新標題
new_paragraph.text = 'B班級選項占比' # 新標題
new_paragraph.font.size = Pt(13) # 新標題字體大小


# chart 4 右上方圖
x, y, cx, cy = Inches(5.5), Inches(0.5), Inches(4), Inches(3)
chart_data = ChartData()
chart_data.categories = ['0', '1-3', '4-6', '7-9']
chart_data.add_series('', (50, 18, 30, 34))
chart = slide.shapes.add_chart(
XL_CHART_TYPE.PIE_EXPLODED, x, y, cx, cy, chart_data
).chart

chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.font.size = Pt(13)
chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'
data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END

chart.has_title = True
chart.chart_title.text_frame.clear()
new_title = chart.chart_title.text_frame.add_paragraph()
new_title.text = '得分占比'
new_title.font.size = Pt(13)

prs.save('test.pptx')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM