Open docx file with password?

2.5k views Asked by At

I am trying to open a word/pdf file that is locked with a password using python, without using a library like python-docx or PDFMiner or such.

I was wondering if anyone knows a way in which you can supply the password to the file so you will be able to read it's content. This is kind of similar to the option of entering a user name and password for a web form.

2

There are 2 answers

0
Roland Smith On BEST ANSWER

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.

0
Harsh On

Yup, there's a way by using which you can open a password-protected docx file but it would need the python-docx module to do this. You can do it this way:

import comtypes.client

word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(document_path, False, True, None, psw)

The parameter psw is the password.