程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python operation pptx file of Python automation series

編輯:Python

ppt Documents are often used in daily office , well ppt Documents require a lot of time and effort to write , But simple ppt file , have access to python Batch build ;

python-pptx Introduce

python-pptx Is used to create and update PowerPoint Of documents python library . Can be used to add slides , Fill in text placeholders , Add image 、 The text box , Add action graphics 、 title 、 Subject properties , Flow charts, etc., and adding slides to tables, etc .

python-pptx Please refer to the official documents for the use of : Document address

python-pptx Simple use :

  • Installation and introduction
    install python-pptx It is recommended to use pip The way , Execute commands directly at the terminal :

    pip install python-pptx

    Import and stock in , The name of the import / export warehouse is not the same as that of the installation python-pptx, It is :

    import pptx

  • Some basic concepts to understand :
    PPT The basic structure is python What do they mean :
    Slide: Slide , Is the page of every page in the presentation .
    Shape: Square box , Box inserted in each slide , It can be shape , It can also be a text box .
    Run: Text block , Usually fewer characters .
    Paragraph: The paragraph , Usually with serial number ㆍ、1. etc. .

  • Write operations
    1、 initialization ppt:

    from pptx import Presentation
    from pptx.util import Inches,Pt
    ppt = Presentation()

    2、 Insert ppt page

    slide_1 = ppt.slides.add_slide(ppt.slide_layouts[0]) Insert a slide , Use layout 0
    shape_1 = slide_1.shapes.placeholders # Get this page ppt All placeholders for
    shape_1[0].text = ‘ Place holder ’ # Yes shape[0] Write content
    shape_1[1].text = ‘Python operation PPT’ # Yes shape[1] Write content

    3、 Save the file :

    ppt.save(‘python.pptx’)

    Complete code :

    def create():''' establish ppt'''from pptx import Presentationfrom pptx.util import Inches,Ptppt = Presentation() # initialization ppt#------------------# first page PPTslide_1 = ppt.slides.add_slide(ppt.slide_layouts[0]) # Insert a slide , Use layout 0shape_1 = slide_1.shapes.placeholders # Get this page ppt All placeholders for shape_1[0].text = ' Place holder ' # Yes shape[0] Write content shape_1[1].text = 'Python operation PPT' # Yes shape[1] Write content #------------------##------------------# The second page PPTslide_3 = ppt.slides.add_slide(ppt.slide_layouts[5]) # Insert a slide , Use layout 5slide_3.shapes.placeholders[0].text = 'test' # Yes, page three PPT One of the first shape Write content picture = slide_3.shapes.add_picture('picture.jpg',left=Inches(3.2),top=Inches(2),width=Inches(3.5),height=Inches(5)) # Insert a picture #------------------##------------------# Page four PPTslide_4 = ppt.slides.add_slide(ppt.slide_layouts[6]) # Insert a slide , Use layout 6table = slide_4.shapes.add_table(rows=2,cols=2,left=Inches(2),top=Inches(2),width=Inches(5),height=Inches(2)).table # Insert table table.columns[0].width = Inches(2) # Set the first 0 The width of the column table.columns[1].width = Inches(3) # Set the first 1 The width of the column # Add content table.cell(0,0).text = 'ppt'table.cell(0,1).text = ' Corresponding library 'table.cell(1,0).text = 'Python'table.cell(1,1).text = 'python-pptx'#------------------#ppt.save('python.pptx')
  • update operation :
    First, create a ppt, Adjust the format , And then through python modify

  • Get a collection of methods for various placeholders

Get this page ppt All placeholders for :shape = slide.shapes.placeholders # Place holder :placeholders
Yes shape[num] Write content :shape[num].text = ‘xx’
Get this page ppt The title of shape:title_shape = slide.shapes.title
To the title shape Write content :title_shape.text = ‘xx’
stay shape[num] Add paragraph :para = shape[num].text_frame.add_paragraph()
Write to the paragraph :para.text = ‘xx’
Set whether the paragraph font is bold :para.font.blod = True
Set paragraph font size :para.font.size = Pt(num)
Set whether to add font underline to paragraphs :para.font.underline = True
Insert text box :textBox = slide.shapes.add_textbox(left,top,width,height)
Insert a picture :picture = slide.shapes.add_picture(‘ picture ’,left,top,width,height)
Insert table :table = slide.shapes.add_table(rows=num,cols=num,left,top,width,height).table
Set table number n The width of the column :table.columns[n].width = Inches(num)
Table cell write content :table.cell(x,y).text = ‘xx’


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved