Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

win32com - Python cannot edit word document

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sorry, I just figured out myself. the issue is when open word 2019, the program has an default view of "read" mode (which prevents the user to modify the document. you can switch the mode at the right bottom corner of word). Then I changed the word default view from read mode to print layout in word option. the script ran without the error.

more information on "How Do You Make Print Layout the Default View Layout in Microsoft Word?"

Link


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...