学习用Python实现PPT的自动化
学习用Python实现PPT的自动化
前言
在日常工作中,我们总是需要创建或修改PPT。但你也可以用Python来创建或修改PPT文件。本文将告诉你如何使用 Python-pptx 模块自动或用PPT模板生成ppt,以及如何通过实例修改现有的PPT。
(文末送福利)
1.Python模块python-pptx。
python-pptx 是Python的一个处理ppt文件的库。它的重点是读写,不能导出,也没有渲染功能。
在使用 python-pptx 模块之前,需要在终端运行命令**pip3 install -i [pypi.doubanio.com/simple/pyth…]
$ pip3 install -i https://pypi.doubanio.com/simple/ python-pptx Looking in indexes: https://pypi.doubanio.com/simple/ Collecting python-pptx Downloading https://pypi.doubanio.com/packages/eb/c3/bd8f2316a790291ef5aa5225c740fa60e2cf754376e90cb1a44fde056830/python-pptx-0.6.21.tar.gz (10.1 MB) |████████████████████████████████| 10.1 MB 2.1 MB/s Preparing metadata (setup.py) ... done Requirement already satisfied: lxml>=3.1.0 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from python-pptx) (4.6.2) Requirement already satisfied: Pillow>=3.3.2 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from python-pptx) (9.0.1) Requirement already satisfied: XlsxWriter>=0.5.7 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from python-pptx) (1.3.7) Using legacy 'setup.py install' for python-pptx, since package 'wheel' is not installed. Installing collected packages: python-pptx Running setup.py install for python-pptx ... done Successfully installed python-pptx-0.6.21
在终端中运行命令 pip show python-pptx 来确认安装。
$ pip show python-pptx Name: python-pptx Version: 0.6.21 Summary: Generate and manipulate Open XML PowerPoint (.pptx) files Home-page: http://github.com/scanny/python-pptx Author: Steve Canny Author-email: python-pptx@googlegroups.com License: The MIT License (MIT) Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages Requires: lxml, Pillow, XlsxWriter Required-by:
2.如何使用python-pptx模块创建一个带有简单文本的PPT。
下面的示例源代码将使用 python-pptx 模块创建一个PPT。
它还将设置PPT的标题和副标题文本,然后将其保存为一个 pptx 文件。
# Import the Presentation class from the pptx module. from pptx import Presentation # Create an instance of the Presentation class. prs = Presentation() # Create the title slide. title_slide_layout = prs.slide_layouts[0] # Add the title slide to the PPT slides array. slide = prs.slides.add_slide(title_slide_layout) # Get the PPT title object. title = slide.shapes.title # Get the PPT subtitle object. subtitle = slide.placeholders[1] # Set the PPT title. title.text = "Hello python-pptx Module!" # Set the PPT title slide sub title. subtitle.text = "pip install python-pptx" # Save the PPT to a .pptx file. prs.save("test.pptx")
3.如何使用python-pptx模块在PPT文件中添加图表。
下面的源代码可以创建一个图表并将其添加到输出的PPT文件中。
# Import the Presentation class from the pptx module. from pptx import Presentation # Import the ChartData class form the pptx.chart.data package. from pptx.chart.data import ChartData # Import the chart type constants variable. from pptx.enum.chart import XL_CHART_TYPE # Import the units type. from pptx.util import Inches # Create the Presentation object to build the PPT file. prs = Presentation() # Add a slide to the PPT file. slide = prs.slides.add_slide(prs.slide_layouts[5]) # Create the ChartData object to save the chart data. chart_data = ChartData() # Save the categories data in an array, the categories data will be displayed in the horizontal x axis . chart_data.categories = ['Java', 'Python', 'JavaScript'] # Save the series data in a tuple, the series data will be displayed in the vertial y axis. chart_data.add_series('Series 1', (19.2, 21.4, 16.7)) # Define the x, y axis unit. x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5) # Add the column chart to the PPT slide. slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data ) # Save the PPT file. prs.save('chart-01.pptx')
4.如何使用PPT模板用Python-pptx模块生成PPT文件。
准备一个PPT模板文件(你可以从网上下载一个,或者自己创建一个PPT模板文件)。
加载PPT模板文件,使用 python-pptx 模块在你的Python源代码中使用指定的幻灯片样式。
向PPT模板文件添加数据,并生成一个新的PPT文件。
下面是源代码的例子。
# Import the pptx module Presentation class. from pptx import Presentation # Import the pptx x axis unit. from pptx.util import Inches from pptx.util import Cm #Inches # Import the ChartData class. from pptx.chart.data import ChartData # Import the Chart type XL_CHART_TYPE. from pptx.enum.chart import XL_CHART_TYPE from pptx.enum.chart import XL_LEGEND_POSITION if __name__ == '__main__': # Create the Presentation object based on the template PPT file. prs = Presentation('template.pptx') # Add the first slide, title only slide. title_only_slide_layout = prs.slide_layouts[5] slide = prs.slides.add_slide(title_only_slide_layout) shapes = slide.shapes # Set the slide title text shapes.title.text = 'Report' # Define the chart table data in an array. name_objects = ["object1", "object2", "object3"] name_AIs = ["AI1", "AI2", "AI3"] val_AI1 = (19.2, 21.4, 16.7) val_AI2 = (22.3, 28.6, 15.2) val_AI3 = (20.4, 26.3, 14.2) val_AIs = [val_AI1, val_AI2, val_AI3] # Define the chart table style. rows = 4 cols = 4 top = Cm(12.5) left = Cm(3.5) #Inches(2.0) width = Cm(24) # Inches(6.0) height = Cm(6) # Inches(0.8) # Add the chart table to the slide. table = shapes.add_table(rows, cols, left, top, width, height).table # Set the table column width. table.columns[0].width = Cm(6)# Inches(2.0) table.columns[1].width = Cm(6) table.columns[2].width = Cm(6) table.columns[3].width = Cm(6) # Set the table text row. table.cell(0, 1).text = name_objects[0] table.cell(0, 2).text = name_objects[1] table.cell(0, 3).text = name_objects[2] # Fill data to the table. table.cell(1, 0).text = name_AIs[0] table.cell(1, 1).text = str(val_AI1[0]) table.cell(1, 2).text = str(val_AI1[1]) table.cell(1, 3).text = str(val_AI1[2]) table.cell(2, 0).text = name_AIs[1] table.cell(2, 1).text = str(val_AI2[0]) table.cell(2, 2).text = str(val_AI2[1]) table.cell(2, 3).text = str(val_AI2[2]) table.cell(3, 0).text = name_AIs[2] table.cell(3, 1).text = str(val_AI3[0]) table.cell(3, 2).text = str(val_AI3[1]) table.cell(3, 3).text = str(val_AI3[2]) # Define the ChartData object. chart_data = ChartData() chart_data.categories = name_objects chart_data.add_series(name_AIs[0], val_AI1) chart_data.add_series(name_AIs[1], val_AI2) chart_data.add_series(name_AIs[2], val_AI3) # Add the chart to the PPT file. x, y, cx, cy = Cm(3.5), Cm(4.2), Cm(24), Cm(8) graphic_frame = slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data ) chart = graphic_frame.chart chart.has_legend = True chart.legend.position = XL_LEGEND_POSITION.TOP chart.legend.include_in_layout = False value_axis = chart.value_axis value_axis.maximum_scale = 100.0 value_axis.has_title = True value_axis.axis_title.has_text_frame = True value_axis.axis_title.text_frame.text = "False positive" value_axis.axis_title.text_frame.auto_size # Save a new PPT file based on the template. prs.save('test_template.pptx')
知道你对Python感兴趣的话,便准备了这套python学习资料,毕竟小编也是用这套方法自学并成功上岸的
对于0基础小白入门:
如果你是零基础小白,想快速入门Python是可以考虑培训的。
一方面是学习时间相对较短,学习内容更全面更集中。
零基础Python学习资源介绍
👉Python学习路线汇总👈
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。 (学习教程文末领取哈)
👉Python必备开发工具👈
温馨提示:篇幅有限,已打包文件夹,获取方式在:文末
👉Python学习视频600合集👈
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
👉实战案例👈
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
👉100道Python练习题👈
检查学习结果。
👉面试刷题👈
资料领取
这份完整版的Python全套学习资料已为大家备好,朋友们如果需要可以微信扫描下方二维码添加,输入"领取资料" 可免费领取全套资料 【 有什么需要协作的还可以随时联系我 】 朋友圈也会不定时的更新最前言python知识。↓↓↓
或者
【 】领取
好文推荐
了解python的前景:
python有什么用: