Last modified: Aug 23, 2026

Put Python Code in PowerPoint

Presenting Python code to an audience is a common task. You may need to show a script during a meeting or a class. Simply copying and pasting code into a slide often looks messy. The formatting breaks, and the text becomes unreadable.

This guide will show you several clean ways to put Python code into PowerPoint. You will learn simple manual methods and a powerful automated approach using the python-pptx library. By the end, your slides will look professional and your code will be easy to read.

Why Direct Copying Fails

When you copy code from an editor and paste it into PowerPoint, the program strips away the syntax highlighting. It also ignores indentation. Python relies on indentation for structure, so this is a big problem. A single missing space can change the meaning of your code.

Furthermore, long lines of code will wrap awkwardly. This makes the slide look cluttered and hard to follow. You need a method that preserves the exact characters and spacing of your script.

The best solutions involve using screenshots or building slides programmatically. Both methods keep your code intact and visually appealing.

Method 1: Use a Screenshot

The quickest way to put Python code in PowerPoint is to take a screenshot. This method is perfect for short snippets. It preserves the exact colors and fonts from your code editor.

First, open your code in an editor with a light background. Dark themes often look bad on projected slides. Then, zoom in so the text is large enough to read from the back of the room. Capture just the code block, not the entire screen.

Once you have the image, insert it into your slide. You can drag and drop the file or use the "Insert" tab in PowerPoint. Resize the image to fit the slide while keeping the aspect ratio. This approach is fast and reliable for static code.

For a detailed guide on handling images in slides, check out our Python PPTX Add Picture Guide. It explains how to automate image placement if you have many slides to create.

Method 2: Use a Text Box with Formatting

If you need to edit the code later, a screenshot is not ideal. Instead, use a text box. First, copy your code from the editor. Then, in PowerPoint, insert a text box and paste the code.

PowerPoint will keep the original line breaks. However, it will remove the leading spaces. You must manually re-add the indentation. This is tedious but works for short scripts.

To make it look better, set the font to a monospace typeface like Consolas or Courier New. These fonts give each character the same width, making the code align properly. You can also change the background color of the text box to a light gray for contrast.

Remember to increase the font size to at least 18 points. This ensures readability from a distance. This method is a good middle ground between a screenshot and full automation.

Method 3: Automate with python-pptx

For presentations with many code slides, manual methods are too slow. The python-pptx library allows you to create entire presentations from a script. You can add slides, insert text boxes, and format them automatically.

First, install the library using pip. Open your terminal and run the command below.


pip install python-pptx

Once installed, you can write a script to create a new slide. The example below adds a title and a text box with code. It uses a monospace font and sets a specific size.


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

# Create a new presentation
prs = Presentation()

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

# Add a title text box
title_box = slide.shapes.add_textbox(Inches(1), Inches(0.5), Inches(8), Inches(1))
title_frame = title_box.text_frame
title_frame.text = "Sample Python Code"

# Add a code text box
code_box = slide.shapes.add_textbox(Inches(1), Inches(1.5), Inches(8), Inches(4))
code_frame = code_box.text_frame
code_frame.text = "def greet(name):\n    print(f'Hello, {name}!')"

# Format the code font
for paragraph in code_frame.paragraphs:
    for run in paragraph.runs:
        run.font.name = 'Consolas'
        run.font.size = Pt(20)

# Save the presentation
prs.save('code_slide.pptx')

This script creates a slide with a title and a code block. The add_textbox method places the text boxes. The loop at the end sets the font for all runs in the code box. This ensures the code looks consistent.

When you run this script, it generates a file named code_slide.pptx. Open it to see the result. The code is cleanly formatted and ready for presentation.

Adding Syntax Highlighting

The previous example uses plain black text. To add colors, you must apply them to each run individually. This is complex but possible. You can define a list of (text, color) tuples for each part of your code.

For example, you can color keywords in blue and strings in green. This manual highlighting makes the slide look professional. However, it requires you to parse your code into tokens. For most users, a screenshot is easier if you need colors.

If you are using a template, the python-pptx library can help. You can load an existing template and add your code to specific placeholders. This saves time on design. Check our Python PPTX: Use Template for Easy Slides guide to learn more.

Best Practices for Readability

No matter which method you choose, follow these rules. First, keep your code snippets short. A slide should show one key concept, not an entire module. Aim for less than 15 lines.

Second, use a large font size. The minimum is 18 points, but 24 is better. You want the audience to read the code without squinting. Third, use a light background for the code area. Dark backgrounds can cause glare on projectors.

Finally, always test your slides on the actual projector. What looks good on your monitor may be too small or too bright in a meeting room. Adjust the font size and colors as needed.

Conclusion

Putting Python code in PowerPoint does not have to be a struggle. You can use a screenshot for quick and visually perfect results. For editable text, use a formatted text box with a monospace font. For automation, the python-pptx library is your best friend.

Each method has its place. Screenshots are great for final presentations. Text boxes are useful for last-minute edits. Automation saves hours when you have many slides to build. Choose the one that fits your workflow.

Remember to keep your code readable and your slides clean. With these techniques, your audience will focus on the logic, not the formatting. Now you can confidently add Python code to your next PowerPoint presentation.