I wanted to convert a file called 'nored.doc' to 'modred.docx', and I could do it using the following code.
import sys
import os
import comtypes.client
os.getcwd()
in_file = os.path.abspath('/Check/')
out_file = os.path.abspath('modred')
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open('nored.doc')
doc.SaveAs(out_file, FileFormat=16)
doc.Close()
word.Quit()
But the problem is that it requires me to paste the input file in the directory "C:\\Windows\\system32\"
. How could I possibly get it working if the input file is present in my working directory???
You can use os.getcwd()
to get current working directory. Then code will look like this:
import sys
import os
import comtypes.client
working_dir = os.getcwd()
#in_file = os.path.abspath(working_dir + '/Check/')
#out_file = os.path.abspath(working_dir + '/modred')
out_file = os.path.abspath('modred')
word = comtypes.client.CreateObject('Word.Application')
#doc = word.Documents.Open('nored.doc')
doc = word.Documents.Open(working_dir + '/nored.doc')
doc.SaveAs(out_file, FileFormat=16)
doc.Close()
word.Quit()