Project Background
Experimental results captured a group of pictures, a larger number, want to be in order to combine the layout, the simpler way is to insert pictures in the PPT layout. But PPT batch inserted pictures, the order is disrupted and does not show the name of the picture, each picture individually adjust the position and size of the time-consuming and laborious, so the idea of using tools for batch operation. In the past, I have learned about python automated office module, relatively speaking, python is also a simple and easy to use language, the project is not expected to consume too much energy, so try to learn and practice a little. (Non-professional learning notes to share, I hope you big brother do not begrudge guidance!)
The data are for 16 sets of experiments, each repeated twice, for a total of 32 images, all of which are square, the
The nomenclature is.
,,, … … … … ,
Needs to be in strict order
infrastructural
mounting
pip install python-pptx
dependencies
Python 2.6, 2.7, 3.3, 3.4, or 3.6
lxml
Pillow
XlsxWriter (to use charting features)
Overview of the base operating code:
import from pptx import Presentation, util prs = Presentation() # Instantiate a ppt presentation object blank_slide_layout = prs.slide_layouts[6] # Instantiate blank templates slide = .add_slide(blank_slide_layout) # Add blank pages to the document img_path = './' # Image path # Set the position and size of the image left = (0) top = (0) width = (4) height = (4) # Insert images into the page pic = .add_picture(img_path, left, top, width, height) ('Automatically generated') # Saved as a file
Step 1: Create a PPT file
from pptx import Presentation prs = Presentation() # Instantiate a ppt presentation object # Intermediate additions of operationally specific additions ('Automatically generated') # Saved as a file
At this point the first pitfall is stepped on and the execution results in an error:AttributeError: module 'collections' has no attribute 'Container'
The reason is the python 3.10 version support problem, at this time in the beginning of the import a dependency package can be solved.
import
Step 2: New Page
prs.slide_layouts is the default page template of the Presentation object, it is an array of 11, you can loop through all the default page templates.
The .add_slide( ) method adds a template page to the file. By default the 7th template is a blank page.
n = len(prs.slide_layouts) print("Number of page templates:", n) for i in range(n): slide_layout = prs.slide_layouts[i] # Instantiate template pages slide = .add_slide(slide_layout) # Add a template page to the file
Adding a blank page on its own requires only the following code:
blank_slide_layout = prs.slide_layouts[6] # Instantiate a blank template page slide = .add_slide(blank_slide_layout) # Add blank pages to the document
Step 3: Add a picture
To add a picture you can use the following method.
pic = .add_picture(img_path, left, top, width, height)
The position and size attributes default to imperial units EMU, which can be converted to centimeters, defined in the following way:
from pptx import util img_path = './' # Image path left = (0) top = (0) width = (4) height = (4)
This gives you a page with an image inserted in the upper left corner.
Gazillion Dot Dot Dot Details
1. Changing the slide page size
The default generated page size is 4 : 3 size page canvas, you can change the size by modifying the properties of the Presentation object as follows:
prs.slide_width = (32) prs.slide_height = (18)
2. Arranging the position of images as required
# Read the list of pictures pic_list = [] for i in listdir(): if '.png' in i: pic_list.append(i) print('Picture List:\n', pic_list) # Set the size of the image width = (4) height = (4) for p in pic_list: # Image path img_path = './' + p # Setting the picture position n = pic_list.index(p) if n < 16: if '-' not in p: top = (0) left = ((n - 1) * 2) else: top = (5) left = (n * 2) else: if '-' not in p: top = (10) left = ((n - 17) * 2) else: top = (15) left = ((n - 16) * 2) # Insert images into the page pic = .add_picture(img_path, left, top, width, height)
final code
import from pptx import Presentation, util from os import listdir # Instantiate a ppt presentation object prs = Presentation() # Resize the page prs.slide_width = (32) prs.slide_height = (19) # Instantiate blank templates blank_slide_layout = prs.slide_layouts[6] # Add blank pages to the document slide = .add_slide(blank_slide_layout) # Read the list of pictures pic_list = [] for i in listdir(): if '.png' in i: pic_list.append(i) print('Picture List:\n', pic_list) # Set the size of the image width = (4) height = (4) for p in pic_list: # Image path img_path = './' + p # Setting the picture position n = pic_list.index(p) if n < 16: if '-' not in p: top = (0) left = ((n - 1) * 2) else: top = (5) left = (n * 2) else: if '-' not in p: top = (10) left = ((n - 17) * 2) else: top = (15) left = ((n - 16) * 2) # Insert images into the page pic = .add_picture(img_path, left, top, width, height) # Saved as a file ('Automatically generated')
Project results chart
summarize
to this article on the python pptx batch to insert pictures into the PPT article is introduced to this, more related python pptx to insert pictures into the PPT content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!