Python comtypes 取消使用密码打开文件

Python comtypes cancel opening of file with password

我有一个 python 脚本,我在其中使用 comtypes 来处理 ms office 文件。但是,我需要处理的一些文档有密码,导致我的脚本在等待输入密码提示时批量触发我的脚本时卡住?

如果文件以编程方式设置密码,是否有取消打开文件的选项?

import comtypes    
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(src_filename)

您可以为此使用 PasswordDocument 关键字参数:

doc = word.Documents.Open(r"d:\test.docx",PasswordDocument="test")

整个片段(使用 python3.6 测试):

from comtypes.client import CreateObject
word = CreateObject('Word.Application')
word.visible = True
doc = word.Documents.Open(r"d:\test.docx",PasswordDocument="test")