你能猜出这是什么代码

编程入门 行业动态 更新时间:2024-10-25 02:20:36

<a href=https://www.elefans.com/category/jswz/34/1764109.html style=你能猜出这是什么代码"/>

你能猜出这是什么代码

from win32com.client import Dispatch        # 导入win32com模块的client包下的函数
import os                        # 导入系统功能模块OS

'''批量生成PowerPoint演示文稿中的幻灯片'''
def PowerPoint():
    ppt = os.path.join(os.getcwd(), "titles.pptx")        # 片头模版文件
    App = Dispatch("PowerPoint.Application")            # 创建PowerPoint应用程序
    App.Visible = True                        # 显示PowerPoint窗口
    Presentation = App.Presentations.Open(ppt)            # 打开模版文件
    # 要显示的内容列表
    itemlist = [("《半壶纱》", "宋佳琦"), ("《洞庭新歌》", "Aurora"), ("《知否、知否》", "无语")]  
    itemlen = len(itemlist)                    # 节目的个数
    offset = 2            # 设置新添加幻灯片的起始页,值为:模版的片头幻灯片个数+1
        # 复制和曲目相同个数的幻灯片(由于要保留格式,所以此处只能通过合并PPT的方式实现)
    for n in range(offset, itemlen + offset):  
        # 将当前目录下的page.pptx中的幻灯片插入到当前PPT文件中
        Presentation.Slides.InsertFromFile(os.path.join(os.getcwd(), "page.pptx"), Presentation.Slides.Count, 1, 1)
    for i in range(offset, itemlen + offset):  # 循环修改每页幻灯片中的曲目名称和表演者
        item = itemlist[i - 2]
        # 修改第一个文本框的内容
        Presentation.Slides(i).Shapes(1).TextFrame.TextRange.Text = item[0]
        # 修改第二个文本框的内容
        Presentation.Slides(i).Shapes(2).TextFrame.TextRange.Text = "表演者:" + item[1]  
    outfile = os.path.join(os.getcwd(), "音乐会PPT.pptx")  # 目标文件
    Presentation.SaveAs(outfile)                    # 另存为新的PowerPoint演示文稿
    App.Quit()                            # 退出PowerPoint应用程序
    print("PPT文件已生成!文件位置为:",outfile)

PowerPoint()  # 调用函数开始生成

更多推荐

你能猜出这是什么代码

本文发布于:2024-02-11 20:32:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1683304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:你能   猜出   代码   这是什么

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!