word自动化脚本只选中部分复选框

2024-05-17 03:20:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个IronPython脚本来选中Word文档中的所有复选框。但是它只检查其中的一些!你知道吗

input.docx是一个Word文档,它包含许多复选框,所有复选框都未选中。 如果我运行脚本,output.docx包含与input.docx相同的内容,只是选中了一些复选框。我的目的是检查所有这些文件。你知道吗

我的剧本有什么问题?你知道吗

# Add the reference for Word
import clr
clr.AddReference("Microsoft.Office.Interop.Word")
import Microsoft.Office.Interop.Word as Word

# Start Word
word_application = Word.ApplicationClass()
word_application.visible = False

# Open the input file
document = word_application.Documents.Open(r"c:\tst\input.docx")

# Check all the checkboxes
for ctrl in document.ContentControls:
    ctrl.Checked = True

# Save and close the output file
document.SaveAs(r"c:\tst\output.docx")
document.Close()

# Quit Word
word_application.Quit()

Tags: the文档import脚本forinputoutputapplication