my goal is to replace some text within an existing word docx. However, I have experienced error while using ReplaceAll function. Could you please help to explain the issue? thank you!
my code:
import win32com.client
constants=win32com.client.constants
wordapp=win32com.client.gencache.EnsureDispatch('Word.Application')
worddoc=wordapp.Documents.Open(templatefile)
wordapp.Visible=False
wordapp.Selection.Find.ClearFormatting
wordapp.Selection.Find.Replacement.ClearFormatting
worddoc.Range(0,0).Select()
selection = wordapp.Selection
selection.Find.ClearFormatting()
selection.Find.Replacement.ClearFormatting()
selection.Find.Forward=True
for dummy in myDict.keys():
selection.Find.Text = dummy
selection.Find.Replacement.Text = myDict[dummy]
selection.Find.Execute(Replace=constants.wdReplaceAll)
worddoc.SaveAs(savefile)
worddoc.Close()
wordapp.Application.Quit()
error message:
Traceback (most recent call last):
File "short_py3_1.py", line 75, in <module>
selection.Find.Execute(Replace=constants.wdReplaceAll)
File "C:Tempgen_py3.80020905-0000-0000-C000-000000000046x0x8x7Find.py", line 45, in Execute
return self._oleobj_.InvokeTypes(444, LCID, 1, (11, 0), ((16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17)),FindText
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'This command is not available.', 'wdmain11.chm', 37373, -2146823683), None)
my conda package:
ca-certificates 2021.1.19 h9f7ea03_0
certifi 2020.12.5 py38h9f7ea03_0
openssl 1.1.1i hc431981_0
pip 20.3.3 py38h9f7ea03_0
pysimplegui 4.34.0 pypi_0 pypi
python 3.8.5 h5fd99cc_1
pywin32 227 py38he774522_1
setuptools 51.3.3 py38h9f7ea03_4
sqlite 3.33.0 h2a8f88b_0
vc 14.2 h21ff451_1
vs2015_runtime 14.27.29016 h5e58377_2
wheel 0.36.2 pyhd3eb1b0_0
wincertstore 0.2 py38_0
zlib 1.2.11 h33f27b4_4
question from:
https://stackoverflow.com/questions/65852064/python-cannot-edit-word-document