打开docx保护文件的最快命令



实际上试图打开非常快的相同的。docx保护的密码文件,而不知道密码,我使用的代码行:

import comtypes.client
word = comtypes.client.CreateObject('Word.Application')
word.Documents.Open(wordPathFile, False, True, None, 'the_pwd')

但是我注意到这种方法非常慢,而且它使用了大量的处理器能力,特别是当我需要大量时间重复这个任务时,而且非常快。

那么有没有更好的方法来完成这个任务呢?这意味着一种首先——也是最重要的——必须更快、消耗更少处理器的方法。
提醒:任务是在不知道密码的情况下尝试快速打开相同的文件。docx密码保护文件。

这是我发现的另一种方法,似乎更快一些:

import msoffcrypto
enc_file = msoffcrypto.OfficeFile(open(wordPathFile, "rb"))  # try start msoffcrypto-tool as OfficeFile with file-name and read-access only
enc_file.load_key(password=pwd_test)  # if password required, take the generated
enc_file.decrypt(open("decrypted.docx", "wb"))  # if password correct, open new file with write-access and copy content in it

在所有情况下,如果有人知道一种更快的方法来完成这项任务(这意味着在不知道密码的情况下快速打开相同的文件.docx密码保护文件),即使这个答案被标记为接受,我也会很高兴阅读它并将他的答案标记为接受。

如果我理解的话,您想在一个程序中多次打开同一个DOCX文件。创建Word.Application的调用可能非常昂贵。相反,你可以打开Word一次,然后多次使用。因为你想保存状态(应用程序对象),一个小的类可能是最方便的。

import comtypes.client
import threading
class DocxFile:
"""Allows for opening a single DOCX file many times from a single
Word.Application.
"""
def __init__(self, filename, pwd):
"""Create a document opener for filename"""
self.word_app = None
self.lock = threading.Lock()
self.filename = filename
self.pwd = pwd

def open_doc(self):
"""Open a new instance of the document. Caller is responsible for
closing."""
if not self.word_app:
with self.lock:
if not self.word_app:
self.word_app = comtypes.client.CreateObject('Word.Application')
return word.Documents.Open(self.filename, False, True, None, self.pwd)
def main():
doc_getter = DocxFile("my_doc.docx", "secret-dont-tell")
for i in range(1_000_000):
doc = doc_getter.open_doc()
etc...

最新更新