【问题标题】:Python comtypes cancel opening of file with passwordPython comtypes 取消使用密码打开文件
【发布时间】:2017-09-28 03:06:19
【问题描述】:

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

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

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

【问题讨论】:

    标签: python-3.x comtypes


    【解决方案1】:

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

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

    整个sn-p(用python3.6测试):

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

    【讨论】:

    • 太棒了!这会为我解决问题并将其包含在 try catch 块中以继续处理其他文件的循环
    • PasswordDocument关键字是否也适用于ppt、msg等其他办公文档?
    猜你喜欢
    • 2018-02-19
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 2020-07-21
    • 2019-09-26
    • 2013-10-27
    • 2022-11-17
    • 1970-01-01
    相关资源
    最近更新 更多