SoFunction
Updated on 2025-04-14

Detailed explanation of examples of using Python for batch operation of PPT

It is a common requirement to unify the font, size, and whether to bold each page of a PPT. In particular, font uniformity is a high-frequency and hot-spot demand. There is a bug in python-pptx, a commonly used library for PPT manipulation of python-pptx, where font modifications can only modify numbers and English letters, and Chinese characters cannot be modified. That is, the attribute can only modify English and digits, and the names of English and digits are also recognized. If the English and numbers in the text box are 'Arial', Chinese characters are Song font, 'Arial' will be returned. Because this package does not have an API for Chinese characters, and this package has not been updated for a long time, the developer has provided a solution to modify the underlying xml of the office file to implement it and modify the typeface attribute of a:ea in the xml. Some people have already implemented this function with the pptx_ea_font package on the Internet.

First install the corresponding package

The packages of pptx and docx are not pptx and docx

pip install python-pptx
pip install python-docx

pptx_ea_font installation method is

pip install pptx_ea_font 

Import the corresponding module

from pptx import Presentation
import pptx_ea_font
from docx import Document
from  import Cm, Pt

1. Modify the font of each page in PPT

Python modification

1. Can modify the font, size, and whether to add boldness

2. Chinese characters for graphics, charts, and tables cannot be modified yet, so this function needs to be added next.

The function is as follows:

def change_ppt_font(ppt_file, new_font,new_size=None,bold=None,line_spacing=None):
    # Open the PPT file    presentation = Presentation(ppt_file)

    # Loop through each slide    for slide in :
        # Loop through each shape in the slide        for shape in :
            # Check whether the shape type is a text box            if shape.has_text_frame:
                # Get text in the text box                text_frame = shape.text_frame
                for paragraph in text_frame.paragraphs:
                    if line_spacing is not None:
                        paragraph.line_spacing = line_spacing
                    for run in :
                        # Modify the font                        pptx_ea_font.set_font(run,new_font)
                        #The following methods can only modify numbers and English                        # = new_font
                        if new_size :
                             = Pt(new_size)
                        if bold is not None:
                             = bold

    # Save the modified PPT file    new_ppt_file = ppt_file.replace(".pptx", "_new.pptx")
    (new_ppt_file)

    print("Font modified!")

The above code can only modify the text box, because the graphics or combined images are msogroup objects.

VBA code library—Modify text boxes and chart fonts to Microsoft Yahei and 1.5 times (commonly used)

The following code is more comprehensive to modify tables and graphics, but cannot modify the text in the graph aggregated by the graph and text box.

Sub ChangeFontInAllSlides()
    Dim oSlide As Slide
    Dim oShape As Shape
    Dim oTable As Table
    Dim oRow As Row
    Dim oCell As Cell
    Dim oTxtRange As TextRange
    Dim oGroup As Shapes
    Dim oChildShape As Shape

    'Travel over all slides in the presentation
     For Each oSlide In
         ' Iterate through all shapes in the slide
        For Each oShape In 
        
            ' If the shape contains a text box
            If  Then
                Set oTxtRange = 
                ' Set the font properties of text in the text box
                With 
                    .Name = "Microsoft Yahe"
                    
                    '.Size = 14
                    '. = RGB(255, 0, 0)
                    '.Bold = True
                    .Italic = False
                    .Underline = False
                End With
                ' Line spacing1.5
                   = 1.5
            End If
            
            ' If the shape is a combination of graphics
            
            If  = msoGroup Then
                ' Directly traverse sub-shapes in combination figures
                For i = 1 To 
                    Set oChildShape = (i)
                    ' If the sub-shape contains text boxes
                    If  Then
                        Set oTxtRange = 
                        ' Set the font properties of text in the text box
                        With 
                            .Name = "Microsoft Yahe"
                            
                            '.Size = 14
                            '. = RGB(255, 0, 0)
                            '.Bold = True
                            .Italic = False
                            .Underline = False
                        End With
                        ' Line spacing1.5
                         = 1.5
                    End If
                Next i
            End If

                       
            ' If the shape contains a table
            If  Then
                Set oTable = 
                ' Iterate through all rows and cells in a table
                For Each oRow In 
                    For Each oCell In 
                        If  Then
                            Set oTxtRange = 
                            ' Set the font properties of text in table cells
                            With 
                                .Name = "Microsoft Yahe"
                                 '.Size = 20
                                 '. = RGB(255, 0, 0)
                                 '.Bold = True
                                .Italic = False
                                .Underline = False
                            End With
                        End If
                    Next oCell
                Next oRow
            End If
        Next oShape
    Next oSlide
End Sub

2. PPT to Word—Put all the words in the text box into word

from pptx import Presentation
from docx import Document

def extract_text_from_shape(shape):
    text = ""
    if hasattr(shape, "text"):
        text +=  + " "
    elif shape.shape_type == 6:  # 6 corresponds to GROUP_SHAPE
        for subshape in :
            text += extract_text_from_shape(subshape)
    return text

def ppt_to_word(presentation_path, output_path):
    # Open PPT    presentation = Presentation(presentation_path)

    # Create Word Document    doc = Document()

    # traverse every slide in PPT    for slide in :
        # Extract the text content of the slide        slide_text = ""
        for shape in :
            slide_text += extract_text_from_shape(shape)

        # Insert text content in Word documents        doc.add_paragraph(slide_text)

    # Save Word Documents    (output_path)

if __name__ == '__main__':
    s = r""
    t = r""
    ppt_to_word(s, t)

3. PPT inserts pictures and modify locations

1. Python PPT insertion picture —Recommended

1. Use the get_image_list(img_dir) function to obtain the PPT file path list, which is convenient for list operations to control the order of pictures inserted into PPT, such as flipping the list, sorting, etc., so that the pictures can be reversed and inserted into PPT in a certain sequence.

2. Use the create_ppt_with_images function to insert the picture into PPT, where the .add_picture(img_file, left, top, width, height) code is the picture to be inserted, left, top, width, height is the coordinates and size of the insertion, left, top represents the insertion position, and is manually set. width, height is the size of the inserted image. You can set it manually or get the size of the slide through width , height = prs.slide_width, prs.slide_height, and then enter the size of the image in proportion.

import os
from pptx import Presentation
from  import Inches


def get_image_list(img_dir):
    """
     Reads the image file name in the folder and returns a list.
     """
    img_files = (img_dir)
    img_path = [(img_dir, f)  for f in img_files if ('.jpg') or ('.png')]  # You can modify it as needed and only select the image format you need    return  img_path


def create_ppt_with_images(img_list, output_ppt_path):
    """
     Insert the given picture list into a new PPT presentation in order.
     """
    prs = Presentation()
    for i, img_file in enumerate(img_list):
        slide = .add_slide(prs.slide_layouts[6])  # Using title and content layout, you can select other layouts as needed        # Set the image position and size to cover the entire slide        left = top = 0
        width ,height = prs.slide_width, prs.slide_height
        pic = .add_picture(img_file, left, top, width, height)
    (output_ppt_path)

if __name__ == '__main__':
    # Please replace 'path_to_your_images' with your image folder path, and '' with the path and file name you want to save PPT    path_img=r"xx"
    path_out=r""
    list_img=get_image_list(path_img)
    create_ppt_with_images(list_img,path_out)

2. VBA PPT insertion picture

Sub InsertPicturesToPPT()
  Dim sld As Slide
  Dim shp As Shape
  Dim i, count, numPicturePerSlide, curSlide As Long
  Dim slideWidth, slideHeight As Single
  Dim autoAddSlide As Boolean

  curSlide = 

  'Use this variable to set each page PPT Number of pictures to be inserted
  numPicturePerSlide = 1
  'Use this variable to set whether to automatically add new pages when there are insufficient pages. PPT Page to insert all selected images,Set as False To cancel this function
  autoAddSlide = True

  fd = Split(FileDialogOpen, vbLf)
  If Left(fd(0), 1) = "-" Then
     "Canceled"
    Exit Sub
  End If

  slideWidth = 
  slideHeight = 

  If autoAddSlide Then
    If ( - curSlide + 1) * numPicturePerSlide < UBound(fd) - LBound(fd) + 1 Then
      total = Int((UBound(fd) - LBound(fd) + 1) / numPicturePerSlide -  + curSlide - 1 + 0.5)
      For i =  + 1 To  + total
        ' Add blank page at the end
         ' i, ppLayoutBlank
        'Add a blank page after the current page
         '  curSlide, ppLayoutBlank
        ' (curSldie - 1).Select
        ' Copy the current page after the current page
        (curSlide).Duplicate
      Next i
    End If
  End If

  count = 0
  For Each sld In 
    ' Skip the hidden ones PPT Page,并跳过在当前Page之前的Page
      & " >= " & curSlide
    If  = msoFalse And  >= curSlide Then
      If count + LBound(fd) > UBound(fd) Then
        ' No picture to insert
        Exit For
      End If

      For i = 1 To numPicturePerSlide
        If count + LBound(fd) <= UBound(fd) Then
          Set shp = ( _
            FileName:=fd(count + LBound(fd)), _
            LinkToFile:=msoFalse, _
            SaveWithDocument:=msoTrue, _
            Left:=0, _
            Top:=0, _
            Width:=-1, _
            Height:=-1 _
          )
          With shp
            .LockAspectRatio = msoTrue  ' Lock aspect ratio
             '.ScaleHeight 0.75, msoTrue
            .Left = slideWidth / numPicturePerSlide * i - .Width / 2
            .Top = (slideHeight - .Height) / 2
            '.ZOrder msoSendToBack   ' 将图片Set as最底层
          End With
          count = count + 1
        Else
          Exit For
        End If
      Next i
    End If
  Next sld

  'MsgBox "Processing finished. Inserted (" & count & ") pictures in total"
  MsgBox "Insert the picture to complete,Insert in total " & count & " A picture"

End Sub

Function FileDialogOpen() As String

  #If Mac Then
    ' Default path
    mypath = MacScript("return (path to desktop folder) as String")

    sMacScript = "set applescript's text item delimiters to "","" " & vbNewLine & _
    "try " & vbNewLine & _
    "set theFiles to (choose file of type {""png"", ""jpg"", ""jpeg"", ""svg"", ""tiff"", ""gif""}" & _
    "with prompt ""Please select one or more pictures to insert"" default location alias """ & _
    mypath & """ multiple selections allowed true)" & vbNewLine & _
    "set applescript's text item delimiters to """" " & vbNewLine & _
    "on error errStr number errorNumber" & vbNewLine & _
    "return errorNumber " & vbNewLine & _
    "end try " & vbNewLine & _
    "repeat with i from 1 to length of theFiles" & vbNewLine & _
    "if i = 1 then" & vbNewLine & _
    "set fpath to POSIX path of item i of theFiles" & vbNewLine & _
    "else" & vbNewLine & _
    "set fpath to fpath & """ & vbNewLine & _
    """ & POSIX path of item i of theFiles" & vbNewLine & _
    "end if" & vbNewLine & _
    "end repeat" & vbNewLine & _
    "return fpath"

    FileDialogOpen = MacScript(sMacScript)

  #Else
    With (msoFileDialogOpen)
      .AllowMultiSelect = True
      .Title = "Please select one or more pictures to insert"
      . "picture", "*.png; *.jpg; *.jpeg; *.svg; *.tiff; *.gif", 1
      If .Show = -1 Then
        FileDialogOpen = ""
        For i = 1 To .
          If i = 1 Then
            FileDialogOpen = .(i)
          Else
            FileDialogOpen = FileDialogOpen & vbLf & .(i)
          End If
        Next
      Else
        FileDialogOpen = "-"
      End If
    End With

  #End If
End Function

3. The location of the picture modification in PPT

Sub test()
'Get all ppt pages
 For Each currentSlide In 'Loop every page
    For Each shp In 
      'type = 13It's a picture  17It's a text box
      If  = 13 Then
        = 10 'set uptopLocation
        = 10  'set upleftLocation
         = 10000 'set up图片高度Location
         = 600
        End If
    Next shp
 
Next currentSlide

End Sub

4. Merge multiple ppts in folders

Notice:

1. It cannot be implemented using the pptx library, and it can design page content copying, etc., which is very troublesome.

Implementation

2. Use new_ppt.SaveAs instead of Save method. The Save method does not accept paths as parameters; it saves files by default at the default location specified when PowerPoint is opened.

import  as win32
import os
import re


def merge_ppt(path:str):
    """
     :param path: file path where ppt is located
     :return: None
     """
    files = (path)
    Application = ("")

     = 1
    new_ppt = ()
    for file in files:
        abs_path = (path, file)
        exit_ppt = (abs_path)
        print(abs_path)
        page_num = exit_ppt.
        exit_ppt.Close()
        new_ppt.(abs_path, new_ppt., 1, page_num)
    new_ppt.SaveAs((path, ''))  # Save under C:\Users\Administrator\Documents\    ()

path=r"C:\xx"
merge_ppt(path)

5. Graphics inserted into shapes in PPT—Python drawing complex graphics

  • By hand-drawing or drawing the system block diagram on paper, and then passing the picture to the multimodal large model
  • Let big models generate ppt graphics Python code
  • Run the code to generate the graphics

1. Insert a rectangle

from pptx import Presentation
from  import Inches, Pt
from  import MSO_SHAPE
from  import RGBColor

# Create a new blank pptpresentation = Presentation()

# Create a new slideslide_layout = presentation.slide_layouts[0]
slide = .add_slide(slide_layout)

# Add a rectangle shape to the slideleft = top = Inches(1)
width = height = Inches(2)
shape = .add_shape(
    MSO_SHAPE.RECTANGLE,
    left, top, width, height
)

# Set the fill color and outline color of the rectangle shapefill = 
()
fill.fore_color.rgb = RGBColor(0x00, 0x00, 0x00)  # black
line = 
 = RGBColor(0xFF, 0x00, 0x00)  # red = Pt(3)  # Set the width of the contour line
# Save the ppt file('')

2. Change the rectangle to three-dimensional

Sub ConvertRectanglesTo3D()
    Dim slide As slide
    Dim shape As shape
    Dim iDepth As Integer ' Set the three-dimensional depth, adjust it as needed
    
     ' Select the slideshow to process,Here is an example of the current slide
    Set slide = 
    
    ' Set three-dimensional depth
    iDepth = 50 ' Sample value, can be adjusted as needed
    
     ' Iterate through all shapes on the slide
    For Each shape In 
        ' Check if the shape is rectangular
        If  = msoShapeRectangle Then
            ' Apply three-dimensional format
            With 
                .Visible = True
                .Depth = iDepth
                ' Other three-dimensional properties can be adjusted as needed,For example, rotation angle, etc.
            End With
        End If
    Next shape
    
    MsgBox "Finished! All rectangles have been converted to 3D."
End Sub

3. Draw the system frame diagram

from pptx import Presentation
from  import Inches, Pt
from  import MSO_SHAPE
from  import RGBColor
from  import PP_ALIGN

def hex_to_rgb(hex_color):
    """Converts a hex color string to an RGBColor object."""
    hex_color = hex_color.lstrip('#')
    return RGBColor(*tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)))

def single_line(presentation, slide, box_contents, box_height, total_length, start_position, color,fontname= 'Microsoft YaHei' ):
    # Set the total length and height of the box    total_length_in = Inches(total_length)
    box_height_in = Inches(box_height)
    
    # Calculate the width of each box    num_boxes = len(box_contents)
    box_width_in = total_length_in / num_boxes
    
    # Set the line color to white    line_color = RGBColor(255, 255, 255)
    
    # Determine the color input type and convert it to RGBColor    if isinstance(color, str) and ('#'):
        fill_color = hex_to_rgb(color)
    elif isinstance(color, (tuple, list)) and len(color) == 3:
        fill_color = RGBColor(*color)
    else:
        raise ValueError("Invalid color format")
    
    # Set the starting position    start_left = Inches(start_position[0])
    start_top = Inches(start_position[1])
    
    # Add boxes in loop    for i, content in enumerate(box_contents):
        # Calculate the position of the current box        left = start_left + i * box_width_in
        top = start_top
        
        # Add rectangle shapes to the slide        shape = .add_shape(
            MSO_SHAPE.RECTANGLE,
            left, top, box_width_in, box_height_in
        )
        
        # Set fill color        fill = 
        ()
        fill.fore_color.rgb = fill_color
        
        # Set line color        line = 
         = line_color
         = Pt(1)
        
        # Add text to the box and set center alignment        if content:
            tf = shape.text_frame
            p = [0]
             = content
             = PP_ALIGN.CENTER


            # Add font settings            run = [0]
            font = 
             =fontname # Set the font to Microsoft Yahei           # = Pt(14) # The font size can be adjusted as needed
# Create multi-line rectangle
#rectangle_rows configuration dictionary, step = 0.2 The interval between each row is 0.2 inches
def multi_line(rectangle_rows, step,output_filename=''):

    presentation = Presentation()
    slide_layout = presentation.slide_layouts[6]
    slide = .add_slide(slide_layout)

    y_offset = 0
    
    for row_id, row_config in rectangle_rows.items():
        start_position = (row_config['start_position'][0], row_config['start_position'][1] - y_offset)
        single_line(presentation, slide, 
                        row_config['box_contents'], 
                        row_config['box_height'], 
                        row_config['total_length'], 
                        start_position, 
                        row_config['color'])
        y_offset += row_config['box_height'] + step
        
    print("Make sure there is no file path PPT open")
    (output_filename)




    

if __name__ == "__main__":

    # Define a dictionary to store the configuration of each line.
    """
   #1 means the first line    1: {  
        # List of contents displayed in each rectangle        'box_contents': ['Box 1', 'Box 2', 'Box 3', 'Box 4'],
        
        # Height of each rectangle (usually inches)        'box_height': 0.8,
        
        # The total length of the entire row of rectangles (usually inches), which determines the width of the rectangles and the spacing between them        'total_length': 8,
        
        # The starting position coordinate (x, y) of the first rectangle in the row, inches        # x coordinate is the position in the horizontal direction, and y coordinate is the position in the vertical direction        'start_position': (1.4, 1.4),
        
        # Fill color of rectangle, use hexadecimal color code        'color': '#1ABC9C' # This is a light green    },

    """

    
    rectangle_rows ={
        
            1: {
                'box_contents': ['Box 1', 'Box 2', 'Box 3', 'Box 4'],
                'box_height': 0.8,
                'total_length': 8,
                'start_position': (1, 4.8),
                'color': '#1ABC9C'
            },
            2: {
                'box_contents': ['Box 1', 'Box 2', 'Box 3', 'Box 4', 'Box 5'],
                'box_height': 0.8,
                'total_length': 8,
                'start_position': (1, 4.8),
                'color': '#00cc99'
            },
            3: {
                'box_contents': ['Box 1', 'Box 2', 'Box 3', 'Box 4', 'Box 5'],
                'box_height': 0.8,
                'total_length': 8,
                'start_position': (1, 4.8),
                'color': '#009dc4'
            },
            4: {
                'box_contents': ['Box 1', 'Box 2', 'Box 3', 'Box 4', 'Box 5'],
                'box_height': 0.8,
                'total_length': 8,
                'start_position': (1, 4.8),
                'color': '#0d98ba'
            },
            5: {
                'box_contents': ['Box 1', 'Box 2', 'Box 3', 'Box 4', 'Box 5'],
                'box_height': 0.8,
                'total_length': 8,
                'start_position': (1, 4.8),
                'color': '#00c5cd'
            },
            6: {
                'box_contents': ['Box 1', 'Box 2', 'Box 3', 'Box 4', 'Box 5'],
                'box_height': 0.8,
                'total_length': 8,
                'start_position': (1, 4.8),
                'color': '#00ced1'
            }


        
     }

    #The interval of each line    step = 0.05
    multi_line(rectangle_rows, step ,output_filename='System Block Diagram.pptx')

6. PPT modify the format of the title text box

The most suitable way to modify the master, but after the master is modified, there are always some boxes that do not change the format. Syndrome Python positioning the title text box

The logic of judging the title is: get the first text box and is located at the top (less than 1/20 of the page from the top).

from pptx import Presentation
from  import Pt
from  import PP_ALIGN
from  import RGBColor

def modify_ppt_title_format(ppt_path, font_name, font_size, font_color):
    """
    RevisePPTFormat of all slide titles in the file。
    
    parameter:
        ppt_path (str): PPTFile path。
        font_name (str): Title Font。
        font_size (int): Font number。
        font_color (tuple): colorRGBvalue。
    """
    # Open an existing PPT file    presentation = Presentation(ppt_path)

    # Get the height of the slide    slide_height = presentation.slide_height
    top_threshold = slide_height / 20  # Define the threshold at the top
    # traverse each slide    for slide in :
        # Find the title shape (usually the first shape is the title)        if :
            first_shape = [0]
            if first_shape.has_text_frame and first_shape.():  # Make sure the shape has a text box and is not empty                # Check if the first shape is at the top                if first_shape.top < top_threshold:
                    text_frame = first_shape.text_frame
                    if len(text_frame.paragraphs) > 0:  # Make sure the paragraph exists                        paragraph = text_frame.paragraphs[0]  # Assume the title is the first paragraph                        
                        # Modify text content (optional)                        # = "New Title" # If you need to modify the title text, uncomment this line
                        # Set font size, color and other properties                        for run in :
                            font = 
                             = font_name  # Change the font                             = Pt(font_size)  # Change the font size                             = RGBColor(*font_color)  # Set color
                        # Set the alignment (optional)                         = PP_ALIGN.CENTER  # Center Align
    # Save the modified PPT to a new file    modified_ppt_path = ''
    (modified_ppt_path)
    print(f"PPTSaved to:{modified_ppt_path}")
PATH=r"   "
# Sample callmodify_ppt_title_format(PATH, font_name='Arial', font_size=36, font_color=(0, 0, 0))

7. Basic knowledge

1. prs.slide_layouts[6] specifies the slide layout, where slide_layouts[6] is the 6th layout and is a whiteboard.

The above is a detailed explanation of the example of using Python to operate PPT in batches. For more information about Python operating PPT, please pay attention to my other related articles!