python 占位符_5分钟让你用Python作出最精美的Powerpoint
介紹
PowerPoint在大多數商業環境中被廣泛使用。本文向您展示如何使用python通過使用python自動創建PowerPoint幻燈片。
5分鐘讓你用Python作出最精美的Powerpoint
在前面的文章里,我們講解了很棒的python庫,用于創建和更新PowerPoint文件:。該API是非常有據可查所以它是很容易使用。唯一棘手的部分是理解PowerPoint文檔結構,包括各種主布局和元素。一旦理解了基礎知識,自動創建自己的PowerPoint幻燈片就相對簡單了。本文將介紹使用pandas讀取和分析某些Excel數據,創建表格以及構建可嵌入PowerPoint文件的圖形的示例。
先來看看制作的精美PPT效果吧:
精美PPT效果1
精美PPT效果2
精美PPT效果3
眼花繚亂了吧,讓我們趕緊向下學習。
PowerPoint文件基礎知識
Python-pptx可以創建空白的PowerPoint文件,但大多數人更愿意使用可以使用您自己的內容自定義的預定義模板。Python-pptx的API非常簡單地支持這個過程,只要您了解模板的一些內容即可。
在深入研究一些代碼示例之前,您需要了解兩個關鍵組件:幻燈片布局和占位符。在下面的例子中,有兩個不同布局的示例以及模板的占位符,可以在其中填充內容。
在下圖中,可以看到我們正在使用布局0,并且幻燈片上的索引1處有一個占位符。
在此圖像中,我們使用布局1來獲得完全不同的外觀。
模板讓工作更輕松,創建了一個簡單的獨立腳本,該腳本采用模板并使用各種元素對其進行標記。
這里是程序源碼,不要著急,看起來很多,但是我們下面會一一講解。
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總結
以上是生活随笔為你收集整理的python 占位符_5分钟让你用Python作出最精美的Powerpoint的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3d旋转相册代码源码_原生 JS 实现
- 下一篇: mysql中文注释语法报错_Linux系