Use Python batch generation PPT version of the certificate of honor example code _python_script home

Use Python batch generation PPT version of the certificate of honor example code

Updated: August 21, 2023 09:44:20 Author: Aperson
Using Python to process PPT files usually requires the use of third-party libraries to simplify the reading, writing and modification of PPT files, this article will introduce you to a small case, using Python to generate PPT version of the certificate of honor, interested students follow the small series to see

First, Python processing PPT

Working with PPT files in Python usually requires the use of third-party libraries to simplify reading, writing, and modifying PPT files. Here are some commonly used Python libraries and their functions:

Python-pptx: This is a popular Python library for creating, reading, and modifying PPTX (Office Open XML format) files. You can use it to create new slides, add text, images, and shapes, and modify the style and layout of your slides.

Python-pptx-template: This library adds support for templates on top of python-pptx. You can use predefined PPTX files as templates, and then modify and generate new presentations based on the templates.

python-com-pptx: This is a Windows COM library for manipulating PPT files. It can interact with the Microsoft PowerPoint application, allowing you to control and modify PPT files through Python scripts.

pptx2pdf: This library is used to convert PPT files to PDF format. It relies on LibreOffice for conversion, so you need to make sure that LibreOffice is installed on your system.

Here is an example of creating a simple slide show using the python-pptx library:

from pptx import Presentation # Create a new PPT file presentation = Presentation() # Add a slide slide = presentation.slides.add_slide(presentation.slide_layouts[5]) # Add title and content to the slide title = slide.shapes.title content = Slide.placeholder [1] title_text = "Welcome to work with powerpoint in python." content.text = "This is an example presentation created using the Python-ppTX library." # Save the PPT file presentation.save("example.pptx")

Note that you need to install the library before running the script. You can install these libraries using pip:

pip install python-pptx pip install python-pptx-template pip install comtypes # Used for python-com-pptx

There are many benefits to working with PPT files in Python:

  • Automation: Python can help you automate repetitive tasks related to PowerPoint, such as creating presentations, updating content, adding slides, or formatting elements. This can save a lot of time and effort.
  • Flexibility: Python provides many libraries and modules that can manipulate PPT files, allowing you to do a variety of operations, including reading, writing, modifying, and extracting data.
  • Library support: There are specific Python libraries such aspython-pptx It can simplify the complexity of working with PowerPoint files and provide a simple API for developers to use.
  • Customization: With Python, you can customize a PowerPoint presentation based on dynamic data or user input. For example, you can combine data from different sources into pre-designed templates to create personalized reports or presentations.
  • Integration: Python can be easily integrated with other tools and systems commonly used in data analysis, data processing, and reporting processes. This way, you can combine powerpoint generation with data processing and analysis to create comprehensive and insightful presentations.
  • Text processing and data extraction: Python's powerful text processing capabilities allow you to extract information from existing PowerPoint presentations. You can search for specific text, extract data from slides, and even generate structured data for further analysis.
  • Collaboration: By working with PPT files in Python, you can facilitate collaboration among team members so that they can programmatically generate and update presentations, reducing manual errors and ensuring consistency.
  • Cross-platform: Python is a cross-platform language, which means that your PPT processing scripts can run on different operating systems, providing flexibility and convenience.
  • Cost savings: Python is an open source language, and libraries for PPT processing are also available for free. This makes Python a cost-effective solution for working with PowerPoint files.

Overall, Python's simplicity, rich library support, and ability to automate tasks make it an excellent choice for working with PowerPoint files, whether you want to automate your presentation, analyze data from slides, or generate dynamic reports.

Second, batch generation of PPT version of the certificate of honor

Technical tools:

Python version :3.9

Code editor: jupyter notebook

At the end of each semester, in addition to asking parents to bring home bedding supplies, there is a "good boy" "certificate of honor." This is not like a big friend or an adult's honorary certificate, must be through fierce competition and only a few people can get. This is for everyone, everyone is a "good boy". If a child does not get this honorary certificate, parents and children are not willing to find the teacher theory, ha ha. My friend's kindergarten has to do this every semester, the whole kindergarten nearly 400 certificates of honor, one by one to fill in a few days. This is not, this time the principal let a friend to fill, really sad dead! So my friend came to me and asked me kindly if I could do it for him. All right, well, when friends ask, you have to give them a breast.

Here, you need to use the third party library 'pptx'. This is a library dedicated to creating and modifying PPT slides (PPT files must have a.ppTX suffix). Let's install the library as follows:

1. Windows logo key + letter key R, enter cmd to run the command input window

2. Enter pip install python-pptx and wait for the download and installation

Sometimes the network is too bad, the download will crash halfway, you can also go directly to the library site (python-pptx · PyPI) with thunder download to the site, and then install. For example, I unzip the downloaded compressed file to disk E, and then open cmd, enter "E:" to enter disk E, and then enter "cd python-pptx-0.6.18" to enter its folder, and finally enter "python setup.py install" to install.

The following is a prompt to complete the installation, and then it is ready to use.

First, you need to set up the template in the first format of the slide master so that it can be called programmatically later:

Here, the template inserts a background picture of the certificate of honor, and then divides the text part into six text boxes (which can be flexibly adjusted according to the actual situation), fill in the content, set the font and size.

After the template is set up in the power point software, it is necessary to use the program to read the corresponding number of the text box in the template, that is, the placeholder ID. Because the placeholder number of a slide in PPT changes constantly with the PPT, it is not necessarily arranged in the order of 1,2,3, and only read it.

from pptx import Presentation prs = Presentation('data\ Certificate of honor.pptx ') slide = prs.slides.add_slide(PRs.slide_layouts [0]) # 0th panel of the first template for ph in slide. Placeholder ders: # Go through all the placeholders on this PPT page phf = ph.placeholder_format # Get the format of the placeholders print(phf.idx) # print the ID number ph.text = str(phf.idx)# write the ID number into the corresponding position of the PPT. So that the following one-to-one correspondence # above read to the placeholder ID convenient after the call prs.save('data\ Certificate of honor template - placeholder number.pptx')

 

Above, we first into the installed 'pptx' library 'Presentation' (presentation/slide) module, and then open the PPT template under the corresponding path, and store the 'prs' variable. Then add a slide from the first template in the master through 'prs.slides.add_slide'. Template format numbers start at 0, so the first format corresponds to '[0]'. Then go through all the placeholders of this PPT page, get the format of its placeholders, and store the 'phf' variable. Then print its ID number via 'phf.idx'. As above, we can see the result. Then the number is written into the corresponding position of the PPT through 'ph.text', so that the following one-to-one correspondence. As can be seen from the figure below, its number is 10 to 15 from top to bottom.

After the template is done and the placeholder ID is determined, you need to read the class and name information of all the children in the kindergarten, and then write. Children's information exists in the existing Excel table, using the 'openpyxl' library to read out. Let's read it.

from openpyxl import load_workbook wb = load_workbook("data\ list.xlsx ") ws = wb.active data={} for row in range(2,ws.max_row+1): class_id = ws['A' + str(row)].value name = ws['B' + str(row)].value data.setdefault(class_id,[]) data[class_id].append(name)
data[' small one '][:10]

Above, the class and name information in the list is stored in the dictionary "data" by pressing "class" as the key and the list composed of "name" as the value. Check the data of the first 10 students in the first class through 'data[' Primary One '][:10]'. Now start to batch write PPT.

import time t0=time.time()# time when the program starts running prs = Presentation('data\ certificate of honor template.pptx') slide_layout = PRs.slide_layouts [0] # Call the set master, because it is the first version of the master, so [0] for class_id in data: for name in data[class_id]: slide = prs.slides.add_slide(slide_layout) # Add a slide based on master's layout # Write content to the slide Slide.Placeholder [10].text = "{} class {} : ".format(class_id,name) # here is a class and name slide.Placeholder [11].text = "Obtain" slide.Placeholder [12].text = "In the first semester of the 2019-2020 school year "The title of" good boy." slide. Placeholder ders[13].text = "This certificate is to be given as an encouragement." Slide.placeholder ders[14].text = "City kindergarten" Slide.Placeholder ders[15].text = "January 2020" prs.save('data\ Certificate of Honor (total).pptx') t1 = Time.time () print(' program time: ',str(round(t1-t0))+' seconds. ')

The awards for 400 children were written in just eight seconds. Above, the 'for' loop gets the class first, and then the second 'for' loop gets the corresponding names under that class. For each name, create a new slide with the 'prs.slides.add_slide' and write the contents according to the placeholder ID we obtained earlier. Except for class and name, everything else is fixed. After writing, save and print. And the PPT is sorted by class, so that the honor certificates of each class will be concentrated together after printing, convenient to distinguish.

The above is the use of Python batch generation PPT version of the certificate of honor example code details, more about Python generation PPT version of the certificate of honor please pay attention to script home other related articles!

Related article

Latest comments