Actually to try to open very fast the same .docx protected by a password file while don't knowing the password, i am using those lines of code:
import comtypes.client
word = comtypes.client.CreateObject('Word.Application')
word.Documents.Open(wordPathFile, False, True, None, 'the_pwd')
But I've noticed that this method is very slow and that it used a lot of the processor capabilities especially if i need to repeat this task a lot of time, and very fast.
So is there a better way to do this task? That means a way that first -and it is the most important- must be faster and after less processor consuming?
Reminder: the task is to try to open very fast the same file .docx password protected file while don't knowing the password.
CodePudding user response:
If I understand, you want to open the same DOCX file many times in a single program. The call to create Word.Application
can be expensive. Instead, you could open Word once, and use it many times. Since you want to save state (the application object), a small class may be the most convenient.
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...
CodePudding user response:
Here the other way i've found and that seems to be slightly faster:
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
In all the cases if someone know a faster way to do this task (that means to open very fast the same file .docx password protected file while don't knowing the password), even if this answer is marked as accepted i will be very happy to read it and to mark his answer as accepted.