Python automation tutorial (4): Automatically generate PPT files Part 2 (dry goods)

Series of tutorials:

Python automation tutorial (1) overview, the first Excel automation

Python Automation Tutorial (2): Excel Automation: Using the pandas library

Python automation tutorial (3): Automatically generate PPT files Part 1

Python automation tutorial (4): Automatically generate PPT files Part 2

Python automation tutorial (5): Automatically generate Word files

Python automation tutorial (6): PDF file processing

In the previous article, I wrote about the method of automatically generating PPT using Excel data in the office library.

In this article, we will continue to introduce other forms of automatically generating PPT using the office library. If you haven’t read the previous article, you probably don’t know how to download and install the office library and generate PPT, so please click here to read the previous article first .

Introduction to office library

    I wrote an office library in python, which is used for office automation. The functions are very strong, including: PPT automatic generation, PPT to long picture, PPT with voice playback, Word automatic generation, Excel data processing, image processing, video processing, Converting office documents to PDF, PDF encryption and decryption, watermarking, etc. are all practical dry goods.

The method of use is extremely simple, and most functions only need one or two lines of code

 Install the office library using pip:

Please install via pip from the command line:

pip install jojo-office

The installation name of the office library is jojo-office

When using, just import office.

import office

Office library dependencies include: python-docx, openpyxl, python-pptx, PyPDF4, reportlab, playsound, etc., which will be installed automatically during installation.

Please click here to download the data and source code files of this article

5. Use dictionaryay as the data source and fill in the PowerPoint template file

Write a template PPT file and write variables. Fill in the dictionary data to generate PPT. However, when the dictionary is used as the data source, the variable writing method is a little different.

The template file template6.pptx is as follows, and the variable name is the key value of the dictionary. Note: For the variable input of the chart, right-click the chart, click the "Edit Data" menu, and write the variable in the first row of the data table.

The python code is as follows:

import office


data = {
    "姓名": "Peter",
    "年龄": 18,
    "相片": "peter.jpg",
    '学习': {
        '课程': ['语文', '数学', '英语'],
        '成绩': [95.3, 68, 75],
        '评价': ['优', '差', '中']
    }
}

# 以 template6.pptx 为模板,创建 report.pptx 文件,  填入data dict数据, 保存
office.open_file("report.pptx", "template6.pptx").fill(data).save()

 Generate PPT as follows:

 illustrate:

This example demonstrates the variable writing of text, pictures, tables, and charts when filling in dictionary data.

6. Use the DataFrame object as the data source and fill in the PowerPoint template file

The DataFrame object of the pandas library can also be used as the data source for filling.

The template file template7.pptx is as follows, 

 Variable writing:

1, the variable {course[0]} represents row 0 of the "course" column of the DataFrame

2. For the table, write in the first line: use the column name of the DataFrame as a variable

For the variable input of the chart, right-click the chart, click the "Edit Data" menu, and write the variable in the first row of the data table. Take the column names of the DataFrame as variables

7. Multiple data sources, fill in PowerPoint files multiple times

Sometimes, there are multiple data sources, and fill() can be called multiple times to fill in the data multiple times.

The template file template8.pptx is as follows, 

 In the template, variable data such as {Sheet1!B1} in the table comes from Excel.

Variable data such as {name} {age} comes from dictionary

The python program is as follows:

import office


person = {"姓名": "Peter", "年龄": 18, "相片": "peter.jpg"}

# 以template8.pptx为模板创建文件,  填入datafile.xlsx文件数据, 再填入person数据,保存
office.open_file("report.pptx", "template8.pptx").fill('datafile.xlsx').fill(person).save()

The running result generates report.pptx as follows:

 It can be seen that the Excel file data and dictionary data are filled in after calling fill() twice. Multiple data sources are combined in one PPT file.

8. Repeatedly generate slides

For example: there is a list of 3 people, a PPT template for one slide, and one slide is generated for each person according to the number of people.

The Sheet1 worksheet of datafile.xlsx has a table

The template file template9.pptx is as follows, 

 In the template, the @repeat flag is added before the variable name, indicating that the variable needs to generate slides repeatedly. There is an @ symbol before the Sheet1!D1 variable, indicating that the variable value is a picture.

The python program is as follows:

# 以 template9.pptx 为模板,创建 report.pptx 文件,  填入datafile.xlsx 文件数据, 保存
office.open_file("report.pptx", "template9.pptx").fill("datafile.xlsx").save()

The generated report.pptx is as follows:

 It can be seen that one slide in the template has become 3 slides, and the data of each row in the Excel table is filled in, among them: The data in Sheet1!D1 is the name of the picture file, and the picture is inserted.

summary:

This article introduces a further method to automatically generate PPT using the office library. Summarize the characteristics of PPT automatic generation:

1. The office library provides a powerful PPT generation function.

2. Write a template PPT file and write variables. Fill in the data to generate PPT.

3. Fill in the data to generate PPT text, tables, charts, and insert pictures

4, The data can be placed in an Excel file. The variable is {worksheet! cell} of Excel.

5, The data can be a dictionary. Variables are key names, such as: {key}.

6, The data can be DataFrame. Variables are column names, such as: {column}.

7. For the data list, it supports repeated generation of slides

8. PPT can be saved as a long picture with a watermark or as a PDF. PPT can be played with voice.

Please click here to download the data and source code files of this article

Sequel:

The office library has many other functions, which will be discussed in the next class.

The office library is still under development, please forgive me for occasional bugs, or provide improvements.

If you are interested in in-depth research, you can see the source code of office.py.

Guess you like

Origin blog.csdn.net/c80486/article/details/126552593
Recommended