使用 docx 库打开有密码的 Word 文档
Opening a Word document that has a password using docx library
我正在尝试打开一个有密码的 word 文档。
我正在使用 docx 包 - 有点旧
from docx import opendocx, getdocumenttext
并进一步
document = opendocx(filename)
我想知道 opendocx 上是否有选项允许它打开受密码保护的 word 文档 - 我知道密码。我在这里查看了 github 存储库:https://github.com/mikemaccana/python-docx 但没有看到选项。我试图避免重写代码以使用更新的包,但这可能是不可避免的。
python-docx doesn't support passwords at the moment. I didn't find it in the code as well, but to be sure, I asked on the python-docx mailing list and received the following reply:
Sorry, no. At least there's no built-in feature for it. I'm not sure how all that works with Word, it might be worth some research.
If it uses the Zip archive's password protection, you could open the .docx file (which is a Zip at the top level), and then do something I'm sure to feed it in. Worst case you could save it as another zip without a password and use that. And of course the interim zip could be a StringIO in-memory file.
If they use their own encryption I expect it would be quite a bit harder :)
Docx 使用自己的加密,而不是 zip 加密。这样只有内部内容需要加密。此处提供了一些有关解密 docx 文件的信息:
如果您不想更改代码,可以使用的一种方法是分叉 docx 包并添加代码来解密 docx 文件。如果您有另一个程序来解密文档,您也可以 shell 出来进行解密。
如果 .docx 仅具有写保护,我认为 docx 包应该按原样工作,因为它可能会忽略 XML 的相关位。对于读取保护,MS-OFFCRYPTO 格式在 Microsoft 网站 https://msdn.microsoft.com/en-us/library/office/cc313071%28v=office.12%29.aspx?f=255&MSPPError=-2147217396 . This document has pseudocode There is a C# implementation at https://www.lyquidity.com/devblog/?p=35 上有详细描述。理论上可以在 python 中实现所有这些,但是在当前包所做的工作之上需要做很多额外的工作,重点是 XML 和文本处理。
我认为目前唯一的选择是使用 MS Word 或 LibreOffice 解密文档,然后使用替代方法以 python.
pywin32 中的API 可以打开受密码保护的.doc 或.docx 文件。功能
win32com.client.Dispatch
可以通过调用WORD App打开一个.doc文件,return一个包含文件信息的对象。比你可以使用对象的方法open
加载数据,你可以将密码传递给第5个参数。
这是我的代码:
word = win32com.client.Dispatch('Word.Application')
word.Visible = False
word.DisplayAlerts = False
doc = word.Documents.Open(document_path, False, True, None, psw)
参数psw
为密码
这个方法好像不支持多线程程序。创建新线程时出现错误。
模块docx
似乎不支持加密文件。
我正在尝试打开一个有密码的 word 文档。
我正在使用 docx 包 - 有点旧
from docx import opendocx, getdocumenttext
并进一步
document = opendocx(filename)
我想知道 opendocx 上是否有选项允许它打开受密码保护的 word 文档 - 我知道密码。我在这里查看了 github 存储库:https://github.com/mikemaccana/python-docx 但没有看到选项。我试图避免重写代码以使用更新的包,但这可能是不可避免的。
python-docx doesn't support passwords at the moment. I didn't find it in the code as well, but to be sure, I asked on the python-docx mailing list and received the following reply:
Sorry, no. At least there's no built-in feature for it. I'm not sure how all that works with Word, it might be worth some research.
If it uses the Zip archive's password protection, you could open the .docx file (which is a Zip at the top level), and then do something I'm sure to feed it in. Worst case you could save it as another zip without a password and use that. And of course the interim zip could be a StringIO in-memory file.
If they use their own encryption I expect it would be quite a bit harder :)
Docx 使用自己的加密,而不是 zip 加密。这样只有内部内容需要加密。此处提供了一些有关解密 docx 文件的信息:
如果您不想更改代码,可以使用的一种方法是分叉 docx 包并添加代码来解密 docx 文件。如果您有另一个程序来解密文档,您也可以 shell 出来进行解密。
如果 .docx 仅具有写保护,我认为 docx 包应该按原样工作,因为它可能会忽略 XML 的相关位。对于读取保护,MS-OFFCRYPTO 格式在 Microsoft 网站 https://msdn.microsoft.com/en-us/library/office/cc313071%28v=office.12%29.aspx?f=255&MSPPError=-2147217396 . This document has pseudocode There is a C# implementation at https://www.lyquidity.com/devblog/?p=35 上有详细描述。理论上可以在 python 中实现所有这些,但是在当前包所做的工作之上需要做很多额外的工作,重点是 XML 和文本处理。
我认为目前唯一的选择是使用 MS Word 或 LibreOffice 解密文档,然后使用替代方法以 python.
pywin32 中的API 可以打开受密码保护的.doc 或.docx 文件。功能
win32com.client.Dispatch
可以通过调用WORD App打开一个.doc文件,return一个包含文件信息的对象。比你可以使用对象的方法open
加载数据,你可以将密码传递给第5个参数。
这是我的代码:
word = win32com.client.Dispatch('Word.Application')
word.Visible = False
word.DisplayAlerts = False
doc = word.Documents.Open(document_path, False, True, None, psw)
参数psw
为密码
这个方法好像不支持多线程程序。创建新线程时出现错误。
模块docx
似乎不支持加密文件。