Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement pptx inserting pictures into PPT in batch by python

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article "python how to achieve pptx batch insert pictures into PPT" most people do not understand, so the editor summed up the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "python how to achieve pptx batch insert pictures into PPT" article bar.

Project background

As a result of the experiment, we took a group of pictures with a large number of pictures. If we want to compose them sequentially, a relatively simple way is to insert pictures in PPT for typesetting. However, after PPT inserts pictures in batches, the order is out of order and the picture name is not displayed, so it is time-consuming and laborious to adjust the position and size of each picture separately, so I want to use the tool to operate in batches. I have learned about the python automation office module in the past, and python is also a relatively easy-to-use language, so the project is not expected to cost too much energy, so try to learn and practice. (non-professional study notes sharing, hope all bosses do not hesitate to guide!

The data are 16 groups of experiments, each group of experiments are repeated twice, a total of 32 pictures, all the pictures are square

The naming method is:

1.pngMagol 1-5.pngMagol 2.pngMagol 2-5.png … ... ... ... 16.pngMagne16-5.png

It needs to be arranged in strict order

Basics

Installation

Pip install python-pptx

Dependence

Python 2.6, 2.7, 3.3, 3.4, or 3.6

Lxml

Pillow

XlsxWriter (to use charting features)

Overview of basic operation codes:

Import collections.abcfrom pptx import Presentation Utilprs = Presentation () # instantiate a ppt presentation object blank_slide_layout = prs.slide_layouts [6] # instantiate blank template slide = prs.slides.add_slide (blank_slide_layout) # add a blank page to the file img_path ='. / 1.png' # Picture path # set the location and size of the picture left = util.Cm (0) top = util.Cm (0) width = util.Cm (4) height = util.Cm (4) # insert a picture in the page pic = slide.shapes.add_picture (img_path Left, top, width, height) prs.save ('automatically generated ppt.pptx') # Save as a file

Step 1: create a PPT file

From pptx import Presentationprs = Presentation () # instantiate a ppt presentation object # add specific operations to add content prs.save ('automatically generated ppt.pptx') # Save as a file

At this time, I stepped on the first hole and reported an error in the execution result:

AttributeError: module 'collections' has no attribute' Container'

The reason is the support of python version 3.10, which can be solved by importing one more dependency package collections.abc at the beginning.

Import collections.abc

Step 2: create a new page

Prs.slide_layouts is the default page template for Presentation objects, an array of 11, and you can loop through all the default page templates.

The prs.slides.add_slide () method adds a template page to the file. The default seventh 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] # instantiation template page slide = prs.slides.add_slide (slide_layout) # add a template page to the file

To add a separate blank page, you only need the following code:

Blank_slide_layout = prs.slide_layouts [6] # instantiate blank template page slide = prs.slides.add_slide (blank_slide_layout) # add a blank page to the file

Step 3: add a picture

You can use the following methods to add pictures

Pic = slide.shapes.add_picture (img_path, left, top, width, height)

The position and size attributes default to the imperial unit EMU, which can be converted to centimeters and are defined as follows:

From pptx import utilimg_path ='. / 1.png' # Picture path left = util.Cm (0) top = util.Cm (0) width = util.Cm (4) height = util.Cm (4)

At this point, you will get a page that inserts a picture in the upper left corner.

Add hundreds of millions of details

1. Change the size of the slide page

The default generated page size is 4: 3 page canvas, which can be changed by modifying the properties of the Presentation object, as follows:

Prs.slide_width = util.Cm (32) prs.slide_height = util.Cm (18)

two。 Arrange the position of the picture as needed

# read picture list 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 picture width = util.Cm (4) height = util.Cm (4) for p in pic_list: # picture path img_path ='. /'+ p # set the picture position n = pic_list.index (p) if n < 16: if'- 'not in p: Top = util.Cm (0) left = util.Cm ((n-1) * 2) else: top = util.Cm (5) left = util.Cm (n * 2) else: if'- 'not in p: Top = util.Cm (10) left = util.Cm ((n-17) * 2) else: top = util.Cm (15) left = util.Cm ((n-16) * 2) # insert the picture pic = slide.shapes.add_picture (img_path) in the page Left, top, width, height) final code import collections.abcfrom pptx import Presentation Utilfrom os import listdir# instantiates a ppt presentation object prs = Presentation () # Resize page prs.slide_width = util.Cm (32) prs.slide_height = util.Cm (19) # instantiate blank template blank_slide_layout = prs.slide_layouts [6] # add blank page slide = prs.slides.add_slide (blank_slide_layout) # read picture list 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 picture width = util.Cm (4) height = util.Cm (4) for p in pic_list: # picture path img_path ='. /'+ p # set the picture position n = pic_list.index (p) if n < 16: if'- 'not in p: Top = util.Cm (0) left = util.Cm ((n-1) * 2) else: top = util.Cm (5) left = util.Cm (n * 2) else: if'- 'not in p: Top = util.Cm (10) left = util.Cm ((n-17) * 2) else: top = util.Cm (15) left = util.Cm ((n-16) * 2) # insert the picture pic = slide.shapes.add_picture (img_path) in the page Left, top, width, height) # Save as file prs.save ('automatically generated ppt.pptx') project result diagram

The above is about the content of this article on "how python implements pptx to insert pictures into PPT in batches". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report