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 = 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):
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')