python根据excel自动生成ppt代码

系统环境

  1. 系统:window
  2. 环境:
  • python 3.7
  • pip
  • pptx
  • openpyxl
  1. 开发工具
  • jupyter
  • PyCharm

引入需要的包

pip install openpyxl
pip install pptx
pip install jupyter

准备文件

python根据excel自动生成ppt代码
代码

import pptx
import openpyxl
import time
import  os
import math
from pptx import Presentation
from openpyxl import load_workbook
from pptx.util import Inches, Pt, Cm
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT
from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE

def addTimeRule(prs):
    blank_slide_layout = prs.slide_layouts[5]
    copied_slide = prs.slides.add_slide(blank_slide_layout)
    copied_slide.shapes[0].text_frame.text = "Time Rule"
    table_shape=copied_slide.shapes.add_table(5,6,Cm(0.77),Cm(3.52),Cm(32.36),Cm(12.44))
    for i in range(5):
        for j in range(6):
            table_shape.table.cell(i, j).alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
    table_shape.table.cell(0,0).text="Type of Speech"
    table_shape.table.cell(0, 0).fill.solid()
    table_shape.table.cell(0, 0).fill.fore_color.rgb=RGBColor(174,163,255)
    table_shape.table.cell(1, 0).text = "Sharing"
    table_shape.table.cell(1, 0).fill.solid()
    table_shape.table.cell(1, 0).fill.fore_color.rgb=RGBColor(211,204,255)
    table_shape.table.cell(2, 0).text = "Prepared Speech"
    table_shape.table.cell(2, 0).fill.solid()
    table_shape.table.cell(2, 0).fill.fore_color.rgb=RGBColor(211,204,255)
    table_shape.table.cell(3, 0).text = "Table Topic Speech"
    table_shape.table.cell(3, 0).fill.solid()
    table_shape.table.cell(3, 0).fill.fore_color.rgb=RGBColor(211,204,255)
    table_shape.table.cell(4, 0).text = "Individual Evaluation"
    table_shape.table.cell(4, 0).fill.solid()
    table_shape.table.cell(4, 0).fill.fore_color.rgb=RGBColor(211,204,255)

    table_shape.table.cell(0, 1).text = "Time Length"
    table_shape.table.cell(0, 1).fill.solid()
    table_shape.table.cell(0, 1).fill.fore_color.rgb=RGBColor(142,166,217)
    table_shape.table.cell(1, 1).text = "20-40 mins"
    table_shape.table.cell(1, 1).fill.solid()
    table_shape.table.cell(1, 1).fill.fore_color.rgb=RGBColor(217,225,242)
    table_shape.table.cell(2, 1).text = "5-7 mins"
    table_shape.table.cell(2, 1).fill.solid()
    table_shape.table.cell(2, 1).fill.fore_color.rgb=RGBColor(217,225,242)
    table_shape.table.cell(3, 1).text = "1-2 mins"
    table_shape.table.cell(3, 1).fill.solid()
    table_shape.table.cell(3, 1).fill.fore_color.rgb=RGBColor(217,225,242)
    table_shape.table.cell(4, 1).text = "2-3 mins"
    table_shape.table.cell(4, 1).fill.solid()
    table_shape.table.cell(4, 1).fill.fore_color.rgb=RGBColor(217,225,242)

    table_shape.table.cell(0, 2).text = "Green(Qualified)"
    table_shape.table.cell(0, 2).fill.solid()
    table_shape.table.cell(0, 2).fill.fore_color.rgb=RGBColor(173,209,149)
    table_shape.table.cell(1, 2).text = "5 mins left"
    table_shape.table.cell(1, 2).fill.solid()
    table_shape.table.cell(1, 2).fill.fore_color.rgb=RGBColor(226,239,218)
    table_shape.table.cell(2, 2).text = "2 mins left"
    table_shape.table.cell(2, 2).fill.solid()
    table_shape.table.cell(2, 2).fill.fore_color.rgb=RGBColor(226,239,218)
    table_shape.table.cell(3, 2).text = "1 min left"
    table_shape.table.cell(3, 2).merge(table_shape.table.cell(4, 2))
    table_shape.table.cell(3, 2).fill.solid()
    table_shape.table.cell(3, 2).fill.fore_color.rgb=RGBColor(226,239,218)
    table_shape.table.cell(4, 2).fill.solid()
    table_shape.table.cell(4, 2).fill.fore_color.rgb=RGBColor(226,239,218)

    table_shape.table.cell(0, 3).text = "Yellow"
    table_shape.table.cell(0, 3).fill.solid()
    table_shape.table.cell(0, 3).fill.fore_color.rgb=RGBColor(255,222,131)
    table_shape.table.cell(1, 3).text = "1 min left"
    table_shape.table.cell(1, 3).merge(table_shape.table.cell(2, 3))
    table_shape.table.cell(1, 3).fill.solid()
    table_shape.table.cell(1, 3).fill.fore_color.rgb=RGBColor(255,242,204)
    table_shape.table.cell(2, 3).fill.solid()
    table_shape.table.cell(2, 3).fill.fore_color.rgb=RGBColor(255,242,204)
    table_shape.table.cell(3, 3).text = "30 secs left"
    table_shape.table.cell(3, 3).merge(table_shape.table.cell(4, 3))
    table_shape.table.cell(3, 3).fill.solid()
    table_shape.table.cell(3, 3).fill.fore_color.rgb=RGBColor(255,242,204)
    table_shape.table.cell(4, 3).fill.solid()
    table_shape.table.cell(4, 3).fill.fore_color.rgb=RGBColor(255,242,204)

    table_shape.table.cell(0, 4).text = "Red"
    table_shape.table.cell(0, 4).fill.solid()
    table_shape.table.cell(0, 4).fill.fore_color.rgb=RGBColor(255,135,135)
    table_shape.table.cell(1, 4).text = "Time's up Finish within 30 secs."
    table_shape.table.cell(1, 4).merge(table_shape.table.cell(2, 4))
    table_shape.table.cell(1, 4).fill.solid()
    table_shape.table.cell(1, 4).fill.fore_color.rgb=RGBColor(255,191,191)
    table_shape.table.cell(2, 4).fill.solid()
    table_shape.table.cell(2, 4).fill.fore_color.rgb=RGBColor(255,191,191)
    table_shape.table.cell(3, 4).fill.solid()
    table_shape.table.cell(3, 4).fill.fore_color.rgb=RGBColor(255,191,191)
    table_shape.table.cell(4, 4).fill.solid()
    table_shape.table.cell(4, 4).fill.fore_color.rgb=RGBColor(255,191,191)

    table_shape.table.cell(0, 5).text = "Ring"
    table_shape.table.cell(0, 5).fill.solid()
    table_shape.table.cell(0, 5).fill.fore_color.rgb=RGBColor(196,194,194)
    table_shape.table.cell(1, 5).text = "Overtime for 30 seconds.Get off stage ASAP."
    table_shape.table.cell(1, 5).merge(table_shape.table.cell(2, 5))
    # mergecell4=mergecell3.merge(table_shape.table.cell(3, 5))
    # mergecell4.merge(table_shape.table.cell(4, 5))
    table_shape.table.cell(1, 5).fill.solid()
    table_shape.table.cell(1, 5).fill.fore_color.rgb=RGBColor(231,230,230)
    table_shape.table.cell(2, 5).fill.solid()
    table_shape.table.cell(2, 5).fill.fore_color.rgb=RGBColor(231,230,230)
    table_shape.table.cell(3, 5).fill.solid()
    table_shape.table.cell(3, 5).fill.fore_color.rgb=RGBColor(231,230,230)
    table_shape.table.cell(4, 5).fill.solid()
    table_shape.table.cell(4, 5).fill.fore_color.rgb=RGBColor(231,230,230)



def updateppt():
    prs = Presentation("E:/anna/base/base-2.pptx")
    workbook = load_workbook(filename="E:/anna/LEP#185会议议程.xlsx")
    sheet = workbook["会议议程前页"]
    topicFlag = 0
    temptopic = " "
    for i in range(13, 47):
        title = sheet.cell(row=i, column=5).value
        author = sheet.cell(row=i, column=6).value
        key_word = sheet.cell(row=i, column=7).value
        suggestion = sheet.cell(row=i, column=8).value
        if author=="Kakarot IP4":
            continue
        else:
            if title=="VOTING":
                blank_slide_layout = prs.slide_layouts[5]
                # add topic and author
                copied_slide = prs.slides.add_slide(blank_slide_layout)
                copied_slide.shapes[0].text_frame.text = title
                copied_slide.shapes.add_picture("E:/anna/base/VOTING.png",Cm(9.38),Cm(3.96),Cm(13.45),Cm(13.45))
                title=="WOTING_TAG"

            else:
                if title == "MASTER SHARING SESSION":
                    topicFlag = i
                # update Presentations
                elif topicFlag == i - 1 :
                    slide = prs.slides[0]
                    slide.shapes[1].text_frame.text = title
                    createTime = slide.shapes[0].text_frame.paragraphs[1]
                    createTime.text = time.strftime("%Y.%m.%d", time.localtime())
                    #Current topic
                elif (author is None or  len(author.strip())==0) or title =="WOTING_TAG" :
                    temptopic=title
                    blank_slide_layout = prs.slide_layouts[0]
                    copied_slide = prs.slides.add_slide(blank_slide_layout)
                    copied_slide.shapes[0].text_frame.text = temptopic
                    body_shape=copied_slide.shapes[1]
                    body_shape.text_frame.text="Shanghai L.E.P. Bilingual Toastmasters Club #185"
                    p = body_shape.text_frame.add_paragraph()
                    p.text=time.strftime("%Y.%m.%d", time.localtime())
                    #
                elif author is not None or len(author.strip())>1 :
                    blank_slide_layout = prs.slide_layouts[0]
                    #add topic and author
                    copied_slide = prs.slides.add_slide(blank_slide_layout)
                    copied_slide.shapes[0].text_frame.text = title
                    copied_slide.shapes[0].top=4217035
                    copied_slide.shapes[0].left = 291465
                    copied_slide.shapes[0].height = 731520
                    copied_slide.shapes[0].width = 11609070

                    #top of page
                    body_shape = copied_slide.shapes[1]
                    body_shape.text_frame.text = "Shanghai L.E.P. Bilingual Toastmasters Club #185 | "+time.strftime("%Y.%m.%d", time.localtime())
                    body_shape.top=325245
                    body_shape.left = 1524000
                    body_shape.height = 914400
                    body_shape.width = 9144000
                    #sub title
                    textbox = copied_slide.shapes.add_textbox(1351280,
                                                 3063240,
                                                 10840720 ,
                                                 731520
                                                 )
                    tf = textbox.text_frame
                    para = tf.paragraphs[0]
                    para.text = temptopic
                    # 设置字体属性
                    font = para.font
                    font.size = Pt(20)  # 大小
                    font.name = 'Arial'  # 字体
                    font.bold = False  # 加粗
                    font.italic = False # 倾斜
                    font.color.rgb = RGBColor(254, 238, 159)  # 黄色
                    #add author
                    textbox = copied_slide.shapes.add_textbox(1524000,
                                                              5452870,
                                                              9144000,
                                                              914400
                                                              )
                    tf = textbox.text_frame
                    para = tf.paragraphs[0]
                    para.text = author
                    para.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
                    # 设置字体属性
                    font = para.font
                    font.size = Pt(28)  # 大小
                    font.name = 'Arial'  # 字体
                    font.bold = False  # 加粗
                    font.italic = False  # 倾斜
                    font.color.rgb = RGBColor(255, 255, 255)
                    if title == "Timer Introduction":
                        addTimeRule(prs)
                else:
                    print( "excel format is not match.Plesse check!")
                # add key word
                if suggestion is not None:
                    blank_slide_layout = prs.slide_layouts[5]
                    # add topic and author
                    copied_slide = prs.slides.add_slide(blank_slide_layout)
                    #add suggession
                    textbox = copied_slide.shapes.add_textbox(Cm(0.77),
                                                              Cm(4.22),
                                                              Cm(32.35),
                                                              Cm(12.11)
                                                              )
                    tf = textbox.text_frame
                    str_len=len(suggestion)
                    d=math.ceil(str_len/62)
                    tempContent="text"
                    for k in range(d):
                        para = tf.paragraphs[k]
                        if k*62<=str_len:
                            tempContent=suggestion[k * 62:min((k+1)*62,str_len)]
                        else:
                            tempContent = suggestion[(k-1) * 62:str_len]
                        para.text = tempContent
                        para.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
                        para.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
                        # 设置字体属性
                        font = para.font
                        font.size = Pt(28)  # 大小
                        font.name = '黑体'  # 字体
                        font.bold = False # 加粗
                        font.italic = False  # 倾斜
                        font.color.rgb = RGBColor(255, 255, 255)
                        tf.add_paragraph()
                    if key_word is not None:
                        textbox1 = copied_slide.shapes[0]
                        tf1 = textbox1.text_frame
                        para1 = tf1.paragraphs[0]
                        para1.text = key_word
                        # 设置字体属性
                        font1 = para1.font
                        font1.size = Pt(36)  # 大小
                        font1.name = '黑体'  # 字体
                        font1.bold = True  # 加粗
                        font1.italic = False  # 倾斜
                        font1.color.rgb = RGBColor(254, 238, 159)  # 黄色

    prs.save("E:/anna/新生成的ppt.pptx")


if __name__ == '__main__':
    updateppt()
上一篇:openpyl 设置单元格格式


下一篇:Python 画五星红旗