[Tutor] Automating MSWord (error if run from command line; no
error if ru n within PythonWin)
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Fri Mar 21 12:17:01 2003
On Fri, 21 Mar 2003, Ezequiel, Justin wrote:
> I am attaching a trimmed-down version of my code.
> I am using MS Word XP, Win XP, Python 2.2.2.
> Works from within PythonWin but not from the command prompt.
>
>
> X:\>python forPost.py omc00055.doc
> Processing omc00055.doc
> Saving as RTF...
> Traceback (most recent call last):
> File "forPost.py", line 29, in ?
> process(sys.argv[1])
> File "forPost.py", line 25, in process
> SaveFile(sFile, sRTFFile, 6)
> File "forPost.py", line 17, in SaveFile
> doc = win32com.client.GetObject(sOldFile)
> File "D:\PYTHON22\lib\site-packages\win32com\client\__init__.py", line 73, in
> GetObject
> return Moniker(Pathname, clsctx)
> File "D:\PYTHON22\lib\site-packages\win32com\client\__init__.py", line 89, in
> Moniker
> dispatch = moniker.BindToObject(bindCtx, None, pythoncom.IID_IDispatch)
> pywintypes.com_error: (-2147287038, 'STG_E_FILENOTFOUND', None, None)
Hi Justin,
I'm not too familiar with the win32 API, but the error message is saying
that it's having a hard time finding the 'omc00055.doc' file. Are you
sure it's located at 'X:\omc00055.doc'?
We can add a small check in the code to see if it can find the source file
before letting Word get at it:
###
def process(sFile):
"""Reads a Microsoft Word document, and writes it as RTF."""
print "Processing", sFile
sRTFFile = GetRTFFile(sFile)
if os.path.isfile(sFile):
print "Saving as RTF..."
SaveFile(sFile, sRTFFile, 6)
else:
print "I can't find the file", os.path.abspath(sFile)
###
The extra code checks to see if the file can be accessed by Python; if
not, at least we'll know that it's a path problem, and not something more
sinister. *grin*
Good luck!