python 占位符_5分钟让你用Python作出最精美的Powerpoint

介绍

PowerPoint在大多数商业环境中被广泛使用。本文向您展示如何使用python通过使用python自动创建PowerPoint幻灯片。

c5bfc64639641960f69c3d5533715651.png

5分钟让你用Python作出最精美的Powerpoint

在前面的文章里,我们讲解了很棒的python库,用于创建和更新PowerPoint文件:。该API是非常有据可查所以它是很容易使用。唯一棘手的部分是理解PowerPoint文档结构,包括各种主布局和元素。一旦理解了基础知识,自动创建自己的PowerPoint幻灯片就相对简单了。本文将介绍使用pandas读取和分析某些Excel数据,创建表格以及构建可嵌入PowerPoint文件的图形的示例。

先来看看制作的精美PPT效果吧:

598142d8ddbca5dbb70e4772e274d470.png

精美PPT效果1

39f122a5853306ed056252eff35f87cb.png

精美PPT效果2

f11a93f7a7bc49c8d31bd516e9021d7f.png

精美PPT效果3

眼花缭乱了吧,让我们赶紧向下学习。

PowerPoint文件基础知识

Python-pptx可以创建空白的PowerPoint文件,但大多数人更愿意使用可以使用您自己的内容自定义的预定义模板。Python-pptx的API非常简单地支持这个过程,只要您了解模板的一些内容即可。

在深入研究一些代码示例之前,您需要了解两个关键组件:幻灯片布局占位符。在下面的例子中,有两个不同布局的示例以及模板的占位符,可以在其中填充内容。

在下图中,可以看到我们正在使用布局0,并且幻灯片上的索引1处有一个占位符。

9be59103ffa6cb2c18f8f9286417ac98.png

在此图像中,我们使用布局1来获得完全不同的外观。

33d7dec0f675e793114142245ce4b5d1.png

模板让工作更轻松,创建了一个简单的独立脚本,该脚本采用模板并使用各种元素对其进行标记。

这里是程序源码,不要着急,看起来很多,但是我们下面会一一讲解。

51984efda6af307221dea03efac1c533.png

analyze_ppt.py

主要模块功能

开始讲解程序主要的功能:

def analyze_ppt(input, output): """ Take the input file and analyze the structure. The output file contains marked up information to make it easier for generating future powerpoint templates. """ prs = Presentation(input) # Each powerpoint file has multiple layouts # Loop through them all and see where the various elements are for index, _ in enumerate(prs.slide_layouts): slide = prs.slides.add_slide(prs.slide_layouts[index]) # Not every slide has to have a title try: title = slide.shapes.title title.text = 'Title for Layout {}'.format(index) except AttributeError: print("No Title for Layout {}".format(index)) # Go through all the placeholders and identify them by index and type for shape in slide.placeholders: if shape.is_placeholder: phf = shape.placeholder_format # Do not overwrite the title which is just a special placeholder try: if 'Title' not in shape.text: shape.text = 'Placeholder index:{} type:{}'.format(phf.idx, shape.name) except AttributeError: print("{} has no text attribute".format(phf.type)) print('{} {}'.format(phf.idx, shape.name)) prs.save(output)

此函数的基本流程是循环并创建源PowerPoint文件中包含的每个布局的示例。然后在每张幻灯片上,它将填充标题(如果存在)。最后,它将遍历模板中包含的所有占位符,并显示占位符的索引以及类型。

如果你想自己尝试一下:

python analyze_ppt.py simple-template.ppt simple-template-markup.ppt

创建自己的PowerPoint

下面不详细涉及pandas具体数据操作,因此在深入研究代码之前确保您对它有了解是有帮助的。让我们从程序的输入和基本shell开始:

from __future__ import print_functionfrom pptx import Presentationfrom pptx.util import Inchesimport argparseimport pandas as pdimport numpy as npfrom datetime import dateimport matplotlib.pyplot as pltimport seaborn as sns# Functions go hereif __name__ == "__main__": args = parse_args() df = pd.read_excel(args.report.name) report_data = create_pivot(df) create_chart(df, "report-image.png") create_ppt(args.infile.name, args.outfile.name, report_data, "report-image.png")在创建命令行args之后,将源Excel文件读入pandas DataFrame。接下来,我们使用该DataFrame作为输入来创建数据的Pivot_table摘要:def create_pivot(df, index_list=["Manager
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值