![]() |
When working with PowerPoint presentations, it can be useful to export only some slides to PDF format. The Python script we present today allows you to do exactly this, in two ways: manually entering the slide range or letting the program automatically identify where the main part of the presentation ends (before the "Annexes").
📦 Requirements
To run the script, you must have installed:
- Python
- The
python-pptx
module:
pip install python-pptx
- The
pywin32
package to interface with PowerPoint:
pip install pywin32
- Microsoft PowerPoint installed on your PC (Windows only).
🐍 pptx to pdf conversion script
import os
import win32com.client
from pptx import Presentation
def getLastSlide(file_path):
prs = Presentation(file_path)
# To get shapes in your slides
slides = [slide for slide in prs.slides]
slide_counter = 0
found = False
for slide in slides:
slide_counter += 1
for shape in slide.shapes:
if shape.has_text_frame:
if shape.text.find("Annex") != -1 or shape.text.find("ANNEX") != -1:
found = True
break
if found:
break
return slide_counter - 1
if __name__ == "__main__":
home_dir = os.path.expanduser("~")
pptx_file_dir = home_dir + "\\Downloads\\"
print("The inserted pptx file must reside in the folder " + pptx_file_dir)
print("\n")
file_name = input("Name of the pptx file (or press Enter to use the default test.pptx): ")
if file_name == "":
file_name = "test.pptx"
pptx_file = pptx_file_dir + file_name
print("\nFile to convert: " + pptx_file)
range_slides = input("\nEnter the range of slides to export to PDF (e.g., 1-10) or type a for identify automatically the last slide: ")
start = end = 0
if range_slides == "a": # automatically retrieve the last slide by searching for the slide with the text "Annex"
end = getLastSlide(file_pptx)
if end <= 0:
range_slides = input("\nI was unable to identify the last slide. Please enter the range of slides from export to PDF (e.g. 1-10): ")
else:
range_slides=f"1-{end}"
start = int(range_slides.split("-")[0])
end = int(range_slides.split("-")[1])
print(f"\nConversion to PDF from slide {start} to slide {end}...")
powerpoint = win32com.client.Dispatch("Powerpoint.Application")
powerpoint.Visible = 1
doc = powerpoint.Presentations.Open(file_pptx)
doc.Slides.Range(range(start, end + 1)).copy()
#new pptx with only the slides range copied from doc pptx
outputPresentation = powerpoint.Presentations.Add()
outputPresentation.Application.CommandBars.ExecuteMso("PasteSourceFormatting")
#save new pptx as PDF
pdfname = os.path.splitext(file_pptx)[0] + ".pdf"
outputPresentation.SaveAs(pdfname, 32)
print("Conversion performed. File " + pdfname + " saved successfully.")
doc.Close()
outputPresentation.Close()
powerpoint.Quit()
os.system('taskkill /F /IM POWERPNT.EXE')
input("\nPress Enter to exit...")
🔍 How the script works
1. File selection
The script searches for the .pptx
file in the user's Downloads
folder. When started, it asks the user to enter the name of the PowerPoint file to convert. If nothing is provided, it will use the test.pptx
file.
home_dir = os.path.expanduser("~")
pptx_file_dir = home_dir + "\\Downloads\\"
file_name = input("File name pptx...")
2. Choosing slides to export
The user can:
- Enter a range (e.g.,
1-10
) - Type
a
to let the script identify the last slide before the "Annexes" (useful if the file contains appendices).
To do this, the getLastSlide(file_path)
function is used, which scans each slide for the word "Annex" or "ANNEX" and returns the index of the previous slide.
if range_slides == "a":
end = getLastSlide(file_pptx)
3. Conversion to PDF
The script uses COM automation (win32com.client
) to interface with PowerPoint and create a new file with only the desired slides.
doc = powerpoint.Presentations.Open(file_pptx)
doc.Slides.Range(range(start, end + 1)).copy()
Then paste the slides into a new presentation, maintaining the original formatting:
outputPresentation = powerpoint.Presentations.Add()
outputPresentation.Application.CommandBars.ExecuteMso("PasteSourceFormatting")
Finally, save the new file as a PDF:
pdfname = os.path.splitext(file_pptx)[0] + ".pdf"
outputPresentation.SaveAs(pdfname, 32)
💡 Why is it useful?
- ✅ Separates the main presentation from any attachments ("Annex")
- ✅ Maintains the original layout and formatting
- ✅ Saves directly to a PDF ready for sharing
⚠️ Limitations
- This only works on Windows, as it uses PowerPoint COM automation.
- It does not support LibreOffice or PowerPoint Online.
- Automatic identification of the final slide only looks for the "Annex" text; it may not work in all situations.
▶️ How to use the script
- Save the script to a file, for example,
pptx_to_pdf.py
- Place the
.pptx
file you want to convert in theDownloads
folder
- Run the script with:
python pptx_to_pdf.py
- Follow the instructions in the terminal.
📌 Conclusion
This script is an excellent example of Python automation with real-world applications, capable of simplifying the process of exporting slides from PowerPoint to PDF. It is particularly useful in business contexts where presentations contain sensitive material or long attachments that should not be included in the final PDF version.
Follow me #techelopment
Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
X: techelopment
Bluesky: @techelopment
telegram: @techelopment_channel
whatsapp: Techelopment
youtube: @techelopment