-3

I'm trying to convert the .pptx files in the folder to .pdf using Python. I did some research and found some codes for this.

import os
import comtypes.client 
import glob

dosya_yolu = "C:/temp/denememrt"

def convert_ppt_to_pdf(input_path, output_path):
    print("İnput Path : " + input_path )
    print("Output Path : " + output_path)

    powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
    presentation = powerpoint.Presentations.Open(input_path) 
    
    presentation.SaveAs(output_path, 32)  #32 is the constant for the PDF format
    presentation.Close()
    powerpoint.Quit()

input_directory = dosya_yolu
output_directory = dosya_yolu

ppt_files = glob.glob(os.path.join(input_directory, "*.pptx"))

for ppt_file in ppt_files:
    pdf_file = os.path.join(output_directory, os.path.basename(ppt_file)[:-5] + ".pdf")
    if os.path.exists(pdf_file):
        ppt_modified = os.path.getmtime(ppt_file)
        pdf_modified = os.path.getmtime(pdf_file)
        if pdf_modified < ppt_modified:
            print(f"Updating {pdf_file}")
            convert_ppt_to_pdf(ppt_file, pdf_file)
    else:
        print(f"Creating {pdf_file}")
        convert_ppt_to_pdf(ppt_file, pdf_file)
        
`

in this code

presentation = powerpoint.Presentations.Open(input_path)

line I get an error that the file path could not be found, but when I print the file path, it finds all the .pptx files in the folder, I think there is a problem in opening the file, I adapted the same code to C# and ran it, I faced the same kind of problem, and there was a problem with the open function.

The error I get is as follows (python)

`    presentation = powerpoint.Presentations.Open(input_path)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_ctypes.COMError: (-2147024894, 'The system cannot find the file specified.', (None, None, None, 0, None))`

I also share the line and error in C# in case it may be useful to you.

`

// Open the source file
                Presentation ppPresentation = ppApp.Presentations.Open(sourceFile);`

`

System.IO.DirectoryNotFoundException
  HResult=0x80070003
  İleti=cannot find the path specified. (HRESULT özel durum döndürdü: 0x80070003)
  Kaynak=ConsoleApp4
  StackTrace:
   konum Microsoft.Office.Interop.PowerPoint.Presentations.Open(String FileName, MsoTriState ReadOnly, MsoTriState Untitled, MsoTriState WithWindow)
   konum PowerPointToPDF.Program.Main(String[] args) C:\Users\msen\source\repos\ConsoleApp4\ConsoleApp4\Program.cs içinde: 28. satır`

Please don't say you couldn't find the file, it finds the file exactly. I think the problem is in the open function but I never figured out what it is, thanks in advance for your help <3

I want to convert a file from pptx to pdf, but I get an error in the Open() function in the code I wrote, I tried other python libraries, but no results

Cansinn
  • 1
  • 4
  • please provide OS, code being used to call the function, including values used in input_path and output_path. – Cody Jan 04 '23 at 10:31

1 Answers1

0

Works exactly as intended.

Change your path to

"C:\\temp\\denememrt"

Luke Bowes
  • 292
  • 2
  • 7
  • thank you so much. That's the whole problem... but what I don't understand is that I did this for word and excel and I didn't specify the file paths in them that way, but they worked, why is it like this in powerpoint? – Cansinn Jan 06 '23 at 14:34
  • By the way this has nothing to do with python-pptx so perhaps you should remove the tag. – Martin Packer Jan 07 '23 at 08:44