Search code examples
pythonpython-pptx

Inserting Text into tables of a power point slide using PPTX package adds a carriage return before desired text


I am using the Python PPTX package to generate tables into a powerpoint presentation. I have a template which is loaded then I generate two tables on the first slide.

Tables are 5,2 each with the first row merged, in the merged row of each table I am attemping to enter text. I can get the text to insert, however, my code seems to add a carriage return before the text, them the top row ends up a different height to the rest of the rows.

My code below

    from pptx.dml.color import RGBColor
    from pptx.util import Cm
    from pptx.util import Pt
    
    from pptx.enum.text import PP_ALIGN
    from pptx.dml.color import RGBColor

    prs = Presentation('pathto/Template.pptx')

    # Select the second slide (index 1 since slide indexes start from 0)
    slide = prs.slides[1]
    
    # Define the position and size of the first table
    x1, y1, cx1, cy1 = Cm(4), Cm(4), Cm(8), Cm(6)
    
    # Add the first table shape to the slide
    shape1 = slide.shapes.add_table(5, 2, x1, y1, cx1, cy1)
    
    # Access the first table object
    table1 = shape1.table
    
    # Merge cells in the top row of the first table
    table1.cell(0, 0).merge(table1.cell(0, 1))
    
    # Get the merged cell in the first table
    merged_cell1 = table1.cell(0, 0)
    
    # Set text and formatting for the merged cell in the first table
    text_frame1 = merged_cell1.text_frame
    text_frame1.clear()  # Clear existing text
    p1 = text_frame1.add_paragraph()
    p1.text = "Background Window"
    p1.alignment = PP_ALIGN.CENTER
    p1.font.name = "Calibri Light"
    p1.font.color.rgb = RGBColor(0, 0, 0)  # Black font color
    p1.font.size = Pt(14)  # Change the font size to the desired value
    
    # Define the position and size of the second table
    x2, y2, cx2, cy2 = Cm(4), Cm(11), Cm(8), Cm(6)
    
    # Add the second table shape to the slide
    shape2 = slide.shapes.add_table(5, 2, x2, y2, cx2, cy2)
    
    # Access the second table object
    table2 = shape2.table
    
    # Merge cells in the top row of the second table
    table2.cell(0, 0).merge(table2.cell(0, 1))
    
    # Get the merged cell in the second table
    merged_cell2 = table2.cell(0, 0)
    
    # Set text and formatting for the merged cell in the second table
    text_frame2 = merged_cell2.text_frame
    text_frame2.clear()  # Clear existing text
    p2 = text_frame2.add_paragraph()
    p2.text = "Activity Tracker"
    p2.alignment = PP_ALIGN.CENTER
    p2.font.name = "Calibri Light"
    p2.font.color.rgb = RGBColor(0, 0, 0)  # Black font color
    p2.font.size = Pt(14)  # Change the font size to the desired value

How do I stop the carriage return from being inserted? I am going to start populating the rest of the tables with additional information so would like to prevent it from occurring there for the remainder of the table as well.


Solution

  • I realized I am using add.Paragraph where I needed reference the cell with .text = "mytext"

    
        # Select the second slide (index 1 since slide indexes start from 0)
        slide = prs.slides[1]
        
        # Define the position and size of the first table
        x1, y1, cx1, cy1 = Cm(4), Cm(4), Cm(8), Cm(6)
        
        # Add the first table shape to the slide
        shape1 = slide.shapes.add_table(5, 2, x1, y1, cx1, cy1)
        
        # Access the first table object
        table1 = shape1.table
        
        # Merge cells in the top row of the first table
        table1.cell(0, 0).merge(table1.cell(0, 1))
        
        # Get the merged cell in the first table
        merged_cell1 = table1.cell(0, 0)
        
        # Set text and formatting for the merged cell in the first table
        text_frame1 = merged_cell1.text_frame
        text_frame1.clear()  # Clear existing text
        merged_cell1.text = "Background Window"
        merged_cell1.alignment = PP_ALIGN.CENTER
        merged_cell1.font.name = "Calibri Light"
        merged_cell1.font.color.rgb = RGBColor(0, 0, 0)  # Black font color
        merged_cell1.font.size = Pt(14)  # Change the font size to the desired value
        
        # Define the position and size of the second table
        x2, y2, cx2, cy2 = Cm(4), Cm(11), Cm(8), Cm(6)
        
        # Add the second table shape to the slide
        shape2 = slide.shapes.add_table(5, 2, x2, y2, cx2, cy2)
        
        # Access the second table object
        table2 = shape2.table
        
        # Merge cells in the top row of the second table
        table2.cell(0, 0).merge(table2.cell(0, 1))
        
        # Get the merged cell in the second table
        merged_cell2 = table2.cell(0, 0)
        
        # Set text and formatting for the merged cell in the second table
        text_frame2 = merged_cell2.text_frame
        text_frame2.clear()  # Clear existing text
        
        merged_cell2.text = "Activity Tracker"
        merged_cell2.alignment = PP_ALIGN.CENTER
        merged_cell2.font.name = "Calibri Light"
        merged_cell2.font.color.rgb = RGBColor(0, 0, 0)  # Black font color
        merged_cell2.font.size = Pt(14)  # Change the font size to the desired value