Opening a Word Document That Has a Password Using Docx Library

Opening a Word document that has a password using docx library

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 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?

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.

Open docx file with password?

According to this webpage the password is just a setting in the settings.xml file in the (zipped) docx file. It does not seem to encrypt the actual contents of the file, because you can delete settings.xml, save the document and still open it...

So you should just be able to read the contents.

Note that this doesn't work with office 2013, where password "protection" seems to have been retired in favor of encryption.

Open a Password-Protected Word File in Java?

You can try it with com4j.

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documents.open2000.aspx

Since there is a parameter called "PasswordDocument" in the "open"-method, I think it is possible to open a password protected file.

Hope this is what you were searching for ;)

Edit: I recorded this Macro in Word.

Documents.Open FileName:="test.doc", ConfirmConversions:= _
False, ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:= _
"hallo", PasswordTemplate:="", Revert:=False, WritePasswordDocument:= _
"hallo", WritePasswordTemplate:="", Format:=wdOpenFormatAuto

So the open method in com4j should look somethin like this (password is "Hallo"):

     _Document document = app.documents().open2000(doc, false, false, false, "hallo", "", false, "hallo", "", WdOpenFormat.wdOpenFormatAuto, false, true);

Open .docx with Apache POI and save it with password

So, I solved the problem by changing EncryptionMode:

// EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard);

I found some information that free (no pay) versions of the MS Word don't support ECMA-376.Agile encryption. So, I changed encryption mode to ECMA-376.Standard and it works for me.
I'm not sure that it's true but it helped in my case.

Hope this will help someone.

Thanks.



Related Topics



Leave a reply



Submit