This is the 31st day of my participation in the August More Text Challenge

In the daily office, PPT is always needed for formal occasions, such as explaining your project plan to the boss, or the year-end duty report at the end of the year, etc. All need to have a beautifully made PPT file to show your ideas and ideas or data to others.

However, the production process of PPT is undoubtedly very tedious, such as batch creation and modification of PPT, the need to write a large number of pictures and words in PPT, accurate insertion of chart data, etc., if manual operation is undoubtedly laborious and cannot guarantee the quality. But using Python to automate PPT is a perfect solution to this problem.

Introduction to the python-PPtx module

Excel has XLRD and XLWT, Word has python-doc, and PPT has its own module, python-PPTX. Python-pptx is a Python library for creating and updating PowerPoint (.pptx) files.

After learning this section, we will finally complete the creation of PPT, as shown in the picture below.



The installation

Python-pptx is a third-party library of Python. Before using it, run the following command to install it:

pip install python-pptx

Copy the code

Using the step

Step 1: Import the python-pptx module

import pptx


Copy the code

Step 2: Create (read) a presentation object

prs=pptx.Presentation('test.pptx')

Copy the code

Step 3: Add slides

import pptx
prs=pptx.Presentation('test.pptx')
slide=prs.slides.add_slide(prs.slide_layouts[0])

Copy the code

Add_slide () is used to add slides. The parameter is to obtain the slide layout to be applied by index. The corresponding relationship between slide layout and PPT is shown in the figure below.

Step 4: Write to the slide

Complete the writing of the required content for each slide in the presentation according to the method provided by PPTX.

Step 5: Generate the PPT file

import pptx prs = pptx.Presentation('test.pptx') ... Prs.save ('test.pptx')Copy the code

Generate the presentation file by saving it with the save () method, which passes in the path of the file you want to save to. This completes the new presentation creation and content writing.

Write the python-pptx operation presentation

Before writing, review the concept of a presentation, as shown below.

The top Presentation object in the figure above is a PPT file we operate, which is usually called a Presentation. A Presentation can contain multiple slides (i.e., multiple slides), and each slide can contain text, graphics, tables, charts and other contents. Next, the common write methods provided by Python-pptx are explained, which are divided according to the type of write.

To add text

To write text on the slide, you can add a text box and write text content in the text box, as shown in the following code:

import pptx prs=pptx.Presentation('test.pptx') slide=prs.slides.add_slide(prs.slide_layouts[0]) Text1 =slide.shapes. Add_textbox (as (5), as (5), as (5), as (5)) text1.text=" I am a textbox" Text1.text_frame.add_paragraph () p1.add_run().text='end' prs.save('test.pptx')Copy the code

The add_slide () method creates a slide, and inserts a textbox in the slide position by add_textbox (). The parameters are left, top, width, height. Set the content of the text box to “I am a text box”, add a paragraph to the text box, set the text content to “I am a paragraph 1”, the add_run () method is the paragraph text append method, the content is directly concatenated after the paragraph, without separate line feed. After execution, the Test.pptx presentation should look like the figure below.

Add graphics

Add optional graphics to the slide, corresponding to the code access, as shown below:

. Shape = slid.shapes. Add_shape (MSO_SHAPE.HEXAGON,Inches(2),Inches(2),Inches(5),Inches(3) Fill =shape.fill. Solid () fill.fore_color.rgb=RGBColor(255,0,0) line= shape.rgb =RGBColor(55,3,5) line.width=Pt(2) prs.save('test.pptx')Copy the code

The add_shape () method is used to add a shape of your choice. The first parameter is the shape type of your choice, which is an enumeration value, and the HEXAGON parameter is used to set the HEXAGON (click here for more shapes). The following parameters are left, top, width, height.

After the optional graph is created, the shape.fill property returns a FillFormat object that contains the fill format properties for the specified graph. The line fill is first set using the solid () method, the fill color is specified using RGB, and the edge style, including border color and width, is set using the shape.line property. After execution, the Test.pptx presentation should look like the figure below.

Add form

Add a table to the slideshow and access it in the corresponding code as follows:

. Table = slid.shapes. Add_table (3,3,Inches(2),Inches(2),Inches(4),Inches(2)) Table.cell (1,0).text='name' table.cell(1,1).text='age' table.cell(1,2).text='class' table.cell(2,0).text=' 3 ' Table.cell (2,1).text='19' table.cell(2,2).text=' 1 '# merge cell=table.cell(0,0) cell1=table.cell(0,2) cell.merge(cell1) Table.cell (0,0).text=' class student information '# cell. Split ()# cancel merge prs.save('test.pptx')Copy the code

Add_table () inserts a table with a specified number of rows and columns. The following parameters are left, top, width, height. After the table is created, the specified cell is obtained through the cell method, and the text property is set to set the text content for the cell. If you want to merge cells, merge them using the merge () method, and cancel the merge using the split () method. After execution, the Test.pptx presentation should look like the figure below.

Add the chart

Add a chart to the slide and access it in the corresponding code, as shown below:

. Slide1 = PRs.slides. Add_slide (PRs.slide_layouts [0]) chart_data=CategoryChartData() Add_series ('Y2020',(300,400,500)); Chart_data.add_series ('Y2019',(400,500,900)) # insert chart into slide Add_chart (xl_chart_type.line,Inches(2),Inches(2),Inches(6),Inches(4),chart_data). Chart =slide1.shapes Chart.has_title =True # Set the title chart.chart_title.text_frame.text=' 1st quarter Sales' # Set the title # Show legend chart.has_legend=True # Set legend position Chart.legend. Position = xl_legend_position. RIGHT step 3: Save PPT file prs.save('test.pptx')Copy the code

The add_slide () method is used to create a new slide, and the add_chart () method is used to insert the chart. The first parameter is the icon type, where LINE is the LINE graph, and the parameters are left, top, width, height, and the data required by the chart. Has_title indicates whether to display the title, has_legend indicates whether to display the legend. After execution, the Test.pptx presentation should look like the figure below.