Python Checking If Files Exist Before Writing
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 funct
Solution 1:
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
defconvert():
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
ifnot 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 thewin32com
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
toinput_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
Post a Comment for "Python Checking If Files Exist Before Writing"