1

I have a file that contains .odt files and I would like to convert them to pdf. My current function works fine, the problem is that even if the file is already converted, the function converts it again, and I do not want to convert a file if it is already converted.

Is there a way to check if the name.odt and name.pdf files already exist?

import sys
import os
import comtypes.client
import glob

def convert():
    for file in glob.glob("*.odt"): # Listing all files
        wdFormatPDF = 17
        in_file = os.path.abspath(file)
        name, ext = os.path.splitext(file)
        suffix = '.pdf'
        os.path.join(name + suffix)
        if not os.path.exists(name): # Pdf file doesn't exist 
            out_file = os.path.abspath(name)

            word = comtypes.client.CreateObject('Word.Application')
            doc = word.Documents.Open(in_file)
            doc.SaveAs(out_file, FileFormat=wdFormatPDF)
            print('the file ' + name +' has been converted')
        else :
            print('all the file are converted')

    doc.Close()
    word.Quit()
cmaher
  • 5,100
  • 1
  • 22
  • 34
user7748633
  • 79
  • 1
  • 1
  • 6
  • 6
    Possible duplicate of [How do I check whether a file exists using Python?](https://stackoverflow.com/questions/82831/how-do-i-check-whether-a-file-exists-using-python) – jpp Feb 11 '18 at 16:56
  • 1
    You are already checking if the file exists in your code. What is the problem? – Jesse Bakker Feb 11 '18 at 16:56
  • it doesn't work. normally when all the files are converted the function should execute the else statement. and print 'all the files are converted. but this is not the case and i don't understand why – user7748633 Feb 11 '18 at 20:26

1 Answers1

0

There are a few things that are not right with your code. Here's the minimal modifications I made to make it work:

import sys
import os
import win32com.client
import glob

def convert():
    word = win32com.client.Dispatch('Word.Application')
    for input_file in glob.glob("*.odt"): # Listing all files
        wdFormatPDF = 17
        in_file = os.path.abspath(input_file)
        name, ext = os.path.splitext(input_file)
        suffix = '.pdf'
        name = name + suffix
        if not os.path.exists(name): # Pdf file doesn't exist 
            out_file = os.path.abspath(name)

            doc = word.Documents.Open(in_file)
            doc.SaveAs(out_file, FileFormat=wdFormatPDF)

            print('the file ' + name +' has been converted')

            doc.Close()

        else:
            print('The file ' + name + ' already exists')

    print('all the file are converted')
    word.Quit()

os.chdir(r"C:\Users\evensf\Documents\Question-48733924\Source")
convert()

Here are my comments about my modifications:

  • For some reason I couldn't understand, I wasn't able to install the comtypes module. So I used the win32com module that comes with Python for Win32 (pywin32) extensions. I think it pretty similar.
  • I opened the Word connector object outside of the loop. You don't really need to open and close it every time you want to open a document. I couldn't make your code work without doing that and it should speedup the execution.
  • I changed your variable name from file to input_file because at one time the name was already assigned to something in Python and that could spell disaster, if I remember correctly. I think this isn't as relevant today, but it's always a good habit to have descriptive name for your variables.
  • Your code seemed to print that all the file are converted when it find an already existant PDF file. I couldn't understand why you would want to do that. So I have put a message when the PDF file has already been created and put your message outside the loop.
  • Since you seem to be working with files in the local directory. I added a command to change the working directory.

But we can go further and simplify your code:

import win32com.client
import pathlib

source_directory = pathlib.Path(r"C:\Users\evensf\Documents\Question-48733924\Source")
wdFormatPDF = 17
destination_suffix = '.pdf'

word_application = win32com.client.Dispatch('Word.Application')
for current_file in source_directory.glob("*.odt"): # Listing all files
    absolute_current_file = current_file.resolve()
    destination_name = absolute_current_file.with_suffix(destination_suffix)
    if destination_name.exists(): 
        print('The file', destination_name, 'already exists. Not converting.')
    else:
        current_document = word_application.Documents.Open(str(absolute_current_file))
        current_document.SaveAs(str(destination_name), FileFormat=wdFormatPDF)

        print('A file has been converted to', destination_name)

        current_document.Close()

print('Finished converting files')
word_application.Quit()
  • I used the pathlib module which has a lot of provisions to simplify your code
EvensF
  • 1,479
  • 1
  • 10
  • 17
  • If you think my answer is appropriate can you consider accepting the answer ? https://stackoverflow.com/help/someone-answers – EvensF Apr 02 '18 at 15:50