Last modified: Aug 23, 2026

Insert Python Code in PowerPoint Easily

Adding Python code to your PowerPoint slides can be tricky. You want it to look clean and readable. This guide shows you simple methods to do it.

We will cover three main ways. You can use the python-pptx library. You can also take screenshots. Or you can use VBA inside PowerPoint.

Each method has its pros and cons. We will explain them clearly. By the end, you will know the best approach for your needs.

Why Insert Python Code in Slides?

Developers often share code in presentations. It helps explain logic during meetings. It is also useful for teaching and documentation.

However, pasting code directly into a text box looks bad. Formatting gets lost. Syntax highlighting disappears. That makes the code hard to read.

Using the right method keeps your code neat. It also makes your slides look professional. Let's explore your options.

Method 1: Using python-pptx Library

The python-pptx library is powerful. It lets you create and modify PowerPoint files. You can add text boxes with code easily.

First, install the library. Use pip in your terminal. This is a simple command to run.


pip install python-pptx

Now, you can write a script. This script will create a new slide. It will add a text box with your code.


from pptx import Presentation
from pptx.util import Inches, Pt

# Create a presentation object
prs = Presentation()

# Add a blank slide layout
slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(slide_layout)

# Set slide dimensions (optional)
slide_width = Inches(10)
slide_height = Inches(5.63)

# Add a text box for code
left = Inches(0.5)
top = Inches(0.5)
width = Inches(9)
height = Inches(4.5)
textbox = slide.shapes.add_textbox(left, top, width, height)
text_frame = textbox.text_frame
text_frame.word_wrap = True

# Your Python code as a string
code = """
def greet(name):
    print(f"Hello, {name}!")

greet("World")
"""

# Add code to the text frame
p = text_frame.paragraphs[0]
p.text = code
p.font.name = 'Consolas'
p.font.size = Pt(14)

# Save the presentation
prs.save('code_slide.pptx')
print("Presentation created successfully!")

This script creates a slide with your code. It uses a monospace font. That makes the code look like a code editor.

You can also style the text box. Change colors, add borders, or adjust spacing. This helps match your presentation theme.

For more advanced styling, consider using a template. You can learn how to use templates effectively in our Python PPTX: Use Template for Easy Slides guide.

Method 2: Taking Screenshots

Screenshots are a quick fix. You can capture code from your editor. Then insert the image into PowerPoint.

Open your code editor. Select the code you want. Take a screenshot using your OS tools. On Windows, use Snipping Tool. On Mac, use Cmd+Shift+4.

Save the screenshot as a PNG file. Then insert it into your slide. This method preserves syntax highlighting perfectly.

However, screenshots have downsides. The text is not editable. If you need to change code, you must redo the screenshot. Also, images can be blurry when resized.

For a quick demo, screenshots work great. For long-term use, editable text is better. You can also combine both methods for best results.

Method 3: Using VBA in PowerPoint

VBA is built into PowerPoint. It allows you to automate tasks. You can write a macro to insert code.

First, enable the Developer tab in PowerPoint. Go to File > Options > Customize Ribbon. Check the Developer box.

Then, open the VBA editor. Press Alt+F11. Insert a new module. Write a subroutine that adds a text box.


Sub AddCodeTextBox()
    Dim slide As Slide
    Dim tbox As Shape
    Dim tf As TextFrame
    
    Set slide = ActivePresentation.Slides(1)
    Set tbox = slide.Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 50, 500, 300)
    Set tf = tbox.TextFrame
    
    With tf.TextRange
        .Text = "def hello():" & vbCrLf & "    print('Hi!')"
        .Font.Name = "Consolas"
        .Font.Size = 12
    End With
End Sub

Run the macro. It will add a text box with your code. You can modify the code string inside the macro.

VBA is powerful but complex. It requires some programming knowledge. It is best for repetitive tasks. If you only need a few slides, use python-pptx instead.

Best Practices for Code Slides

No matter the method, keep your code readable. Use a monospace font like Consolas. Set a reasonable font size, around 14-18 points.

Keep lines short. Avoid long lines that wrap. Break code into multiple lines if needed. This prevents awkward breaks.

Use syntax highlighting if possible. Tools like Pygments can generate colored HTML. You can then insert it as an image or styled text.

Also, consider adding a dark background. Light text on dark background reduces glare. It looks modern and professional.

Always test your slides on a projector. What looks good on a monitor may be hard to read from afar. Adjust font sizes accordingly.

Automating with python-pptx

The python-pptx library is great for automation. You can generate many slides at once. This saves time for large presentations.

You can also read code from files. This keeps your slides in sync with your actual codebase. No more copy-paste errors.

Here is an example of reading code from a file:


from pptx import Presentation
from pptx.util import Inches, Pt

def add_code_slide(prs, code_file):
    with open(code_file, 'r') as f:
        code = f.read()
    
    slide_layout = prs.slide_layouts[6]
    slide = prs.slides.add_slide(slide_layout)
    
    txBox = slide.shapes.add_textbox(Inches(0.5), Inches(0.5), Inches(9), Inches(5))
    tf = txBox.text_frame
    tf.text = code
    tf.paragraphs[0].font.name = 'Consolas'
    tf.paragraphs[0].font.size = Pt(12)
    
    return slide

prs = Presentation()
add_code_slide(prs, 'my_script.py')
prs.save('auto_slides.pptx')

This script reads a Python file and adds it to a slide. You can call the function multiple times for different files.

For more control over images and layouts, check our Python PPTX Add Picture Guide. It shows how to add visual elements to your slides.

Styling Your Code Blocks

Plain text is functional but boring. You can add a background color to your text box. This makes code stand out.

In python-pptx, you can set fill color. Use the fill property of the shape. Here is an example:


# Assuming txBox is your shape
txBox.fill.solid()
txBox.fill.fore_color.rgb = RGBColor(0x2D, 0x2D, 0x2D)  # Dark gray

You also need to import RGBColor from pptx.dml.color. This gives your code a nice background.

You can also add a border. Use the line property. Set its color and width. This frames your code nicely.

Remember to keep the text color light if the background is dark. White or light gray works best. This ensures readability.

Common Issues and Fixes

Sometimes code appears with weird spacing. This happens when tabs are mixed with spaces. Use only spaces or only tabs consistently.

Long lines may get cut off. Reduce the font size or shorten the code. You can also enable text wrapping in the text box.

Special characters like < or > might display incorrectly. In python-pptx, they are handled automatically. But in VBA, you may need to escape them.

If your code has emojis or Unicode, ensure the font supports them. Consolas does not support all Unicode characters. Try using a font like "DejaVu Sans Mono" instead.

Conclusion

Inserting Python code in PowerPoint is easy with the right tools. The python-pptx library offers the most flexibility. It allows automation and styling.

Screenshots are quick for one-off slides. VBA is good for PowerPoint-only workflows. Choose based on your needs.

Remember to format your code for readability. Use monospace fonts and appropriate colors. Test your slides on different screens.

With these techniques, you can create professional code slides. Your audience will appreciate the clarity. Start using these methods today.