from pptx import Presentation
from pptx.util import Pt,Cm
from pptx.dml.color import RGBColor
from pptx.enum.text import MSO_ANCHOR
from pptx.enum.text import PP_ALIGN
ppt = Presentation()
slide = ppt.slides.add_slide(ppt.slide_layouts[6])
# 设置表格位置和大小
left, top, width, height = Cm(6), Cm(6), Cm(6), Cm(5)
# 表格行列数,和大小
shape = slide.shapes.add_table(6, 7, left, top, width, height)
# 获取table对象
table = shape.table
# 设置列宽
table.columns[0].width = Cm(3)
table.columns[1].width = Cm(2.3)
table.columns[2].width = Cm(2.3)
table.columns[3].width = Cm(1.3)
table.columns[4].width = Cm(1.3)
table.columns[5].width = Cm(1.3)
table.columns[6].width = Cm(2.1)
# 设置行高
table.rows[0].height = Cm(1)
# 合并首行
table.cell(0, 0).merge(table.cell(0, 6))
# 填写标题
table.cell(1, 0).text = "时间"
table.cell(1, 1).text = "阶段"
table.cell(1, 2).text = "执行用例"
table.cell(1, 3).text = "新增问题"
table.cell(1, 4).text = "问题总数"
table.cell(1, 5).text = "遗留问题"
table.cell(1, 6).text = "遗留致命/" \
"严重问题"
# 填写变量内容
table.cell(0, 0).text = "产品1"
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"]]
# 修改表格样式
for rows in range(6):
for cols in range(7):
# Write column titles
if rows == 0:
# 设置文字大小
table.cell(rows, cols).text_frame.paragraphs[0].font.size = Pt(15)
# 设置字体
table.cell(rows, cols).text_frame.paragraphs[0].font.name = '微软雅黑'
# 设置文字颜色
table.cell(rows, cols).text_frame.paragraphs[0].font.color.rgb = RGBColor(255, 255, 255)
# 设置文字左右对齐
table.cell(rows, cols).text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
# 设置文字上下对齐
table.cell(rows, cols).vertical_anchor = MSO_ANCHOR.MIDDLE
# 设置背景为填充
table.cell(rows, cols).fill.solid()
# 设置背景颜色
table.cell(rows, cols).fill.fore_color.rgb = RGBColor(34, 134, 165)
elif rows == 1:
table.cell(rows, cols).text_frame.paragraphs[0].font.size = Pt(10)
table.cell(rows, cols).text_frame.paragraphs[0].font.name = '微软雅黑' # 字体名称
table.cell(rows, cols).text_frame.paragraphs[0].font.color.rgb = RGBColor(0, 0, 0)
table.cell(rows, cols).text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
table.cell(rows, cols).vertical_anchor = MSO_ANCHOR.MIDDLE
table.cell(rows, cols).fill.solid()
table.cell(rows, cols).fill.fore_color.rgb = RGBColor(204, 217, 225)
else:
table.cell(rows, cols).text = content_arr[rows - 2][cols]
table.cell(rows, cols).text_frame.paragraphs[0].font.size = Pt(10)
table.cell(rows, cols).text_frame.paragraphs[0].font.name = '微软雅黑' # 字体名称
table.cell(rows, cols).text_frame.paragraphs[0].font.color.rgb = RGBColor(0, 0, 0)
table.cell(rows, cols).text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
table.cell(rows, cols).vertical_anchor = MSO_ANCHOR.MIDDLE
table.cell(rows, cols).fill.solid()
table.cell(rows, cols).fill.fore_color.rgb = RGBColor(204, 217, 225)
ppt.save('./办公自动化/files/ppt_test.pptx')