Opening a Word document that has a password using docx library
Question: I'm trying to open a word document that has a password.

I'm using docx package - a bit old

    
    
    from docx import opendocx, getdocumenttext
    

and further on

    
    
     document = opendocx(filename)
    

I was wondering if there were options on the opendocx to allow it to open
password protected word documents - I do know the password. I checked the
github repo here: <https://github.com/mikemaccana/python-docx> but didn't see
an option. I'm trying to avoid rewriting the code to use a newer package but
that may be inevitable.


Answer: [python-docx](https://github.com/mikemaccana/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](https://groups.google.com/forum/#!forum/python-docx) and [received the
following reply](https://groups.google.com/forum/#!topic/python-
docx/k5dLjcDdDS0):

> 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 uses their own encryption, not zip encryption. This way only the internal
contents need to be encrypted. Some info on decrypting docx files is available
here:

  * [Super User: How can I unlock a Microsoft .docx document?](http://superuser.com/questions/486844/how-can-i-unlock-a-microsoft-docx-document)

One approach that you can use if you don't want to change your code is to fork
the docx package and add code to decrypt the docx file. If you had another
program to decrypt the document, you could also shell out to do the
decryption.

