Home > Mobile >  Open a Microsoft Office password protected file on any operating system
Open a Microsoft Office password protected file on any operating system

Time:06-30

My question is very simple but i am really stuck. I am a beginner in python programming and an absolute noob on MAC.
What i want to do is to open in python on a mac a word/docx file that is protected by a password (i all ready know the password).
So I have this line of code that works on windows:

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

But they don't work on MAC, i get this error

Import Error: cannot import name 'COMError' from '_ctypes'

I ve searched for a solution on the web and i didn't found one. I am just asking what are the equivalent of those three lines on macos.
It's very important to me and i am running out off time, so you are my last hope thank you.

CodePudding user response:

So i found this library:
https://pypi.org/project/msoffcrypto-tool/
That is cross platform and that work good for me.

Here a code exemple to open any encrypted microsoft office file on any platform:

import msoffcrypto

enc_file = msoffcrypto.OfficeFile(open(encrypted_file_path, "rb"))  # try start msoffcrypto-tool as OfficeFile with file-name and read-access only
enc_file.load_key(password='password_of_the_file')  # 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

I hope it will help other people with the same problem.

  • Related