I’m trying to use python’s win32com library to modify an exsit word file.
the word file is saved as 97-2003 format, and I have office 2010 installed on my computer,
here is my code:
import win32com
from win32com.client import Dispatch, constants
msword = Dispatch('Word.Application')
doc = msword.Documents.Open(FileName = "Template.doc")
msword.Quit()
and the program just crashed when it opens Template.doc
D:\DongleMaker>DongleMaker.py
Traceback (most recent call last):
File "D:\DongleMaker\DongleMaker.py", line 18, in <module>
doc = msword.Documents.Open(FileName = "Template.doc")
File "C:\Python27\lib\site-packages\win32com\gen_py\00020905-0000-0000-C000-000000000046x0x8x5\Documents.py", line 96, in Open
, Visible, OpenAndRepair, DocumentDirection, NoEncodingDialog, XMLTransform
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Word', u'This file could not be found.\r ("C:\\...\\My Documents\\Template.doc")', u'
wdmain11.chm', 24654, -2146823114), None)
D:\DongleMaker>
Is this because win32com don’t support office 2010?
This is because Word looks for your .doc file in the My Document directory. Just provide a full path to Template.doc (which I believe is in the DongleMaker dir) and Word will launch successfully. Mine just did.