pythonpython-pptx

How to convert PPTX to PDF on a Non-Windows machine


I'm sure many have come across this question, but I couldn't find any solutions for non-windows machines.

I need to prepare a Python script that can run on Linux (multiple versions) as well as On Windows machines. The script should read a PPT file, and convert it into a PDF, then convert the PDF into images. I can achieve the second part (PDF to images), but I can't achieve the first part (PPT to PDF).

Can anyone please help me to achieve this?

I tried Spire and Aspose, but both of them are commercial and we need to pay. Python pptx doesn't support conversion. Win32 is not available in Linux.

I am at a dead end.

This is the code I used to achieve the above (both the first and second parts) using the spire library, but it doesn't convert more than 10 pages.

!pip install Spire.Presentation

# Function to convert PPT to Images
def ppt_to_images(ppt_file, destination_folder):
    presentation = Presentation()
    presentation.LoadFromFile(ppt_file)
    presentation_name = ppt_file.split('/')[-1].split('.')[0]
    #Save PPT document to images
    for i, slide in enumerate(presentation.Slides):
        file_name = presentation_name + '_' + str(i) + ".png"
        image = slide.SaveAsImage()
        image.Save(destination_folder + '/' + file_name)
        image.Dispose()
    presentation.Dispose()
    return None

ppt_to_images("/content/drive/MyDrive/Colab Notebooks/DummyPPTs/dummy_powerpoint.pptx", OUTPUT_FOLDER)

Calling the above function using

ppt_to_images("/content/drive/MyDrive/Colab Notebooks/DummyPPTs/dummy_powerpoint.pptx", OUTPUT_FOLDER)

Solution

  • Try this code to convert any pptx file to pdf

     import subprocess
     import os
    
     def convert_pptx_to_pdf(input_path, output_dir):
     if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    
     # Construct the command to convert pptx to pdf
      command = [
        'libreoffice',
        '--headless',
        '--convert-to',
        'pdf',
        '--outdir',
        output_dir,
        input_path
    ]
    
    # Execute the command
    subprocess.run(command, check=True)
    print(f'Converted {input_path} to PDF and saved in {output_dir}')
    
    # Specify the path to the .pptx file and the output directory
    pptx_path = 'presentation.pptx'
    output_directory = 'Desktop'
    
    # Convert the .pptx file to PDF
    convert_pptx_to_pdf(pptx_path, output_directory)