I want to open password protected xls or xlsx using python. Generally I use xlrd to process xls or xlsx files but it cannot open password protected excel files. I tried to use pywin32 but I was not able to install it on my Linux system.
Asked
Active
Viewed 2,337 times
2
-
Have you tried to install pywin32 using a whl file? ... http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32 ... `pip install file_name.whl` – jesterjunk Dec 28 '15 at 10:02
-
If no Python library supports this out of the box, perhaps you would be willing to write another program using [this](http://poi.apache.org/encryption.html) Apache Java library to decrypt the files before processing them with Python (or use some Java/Python bridge to embed the library)? – steinar Dec 28 '15 at 10:04
-
@jesterjunk yes I did.. I am getting this pywin32-219-cp27-none-win32.whl is not a supported wheel on this platform. – Vishal Joshi Dec 28 '15 at 10:50
-
I somehow completely overlooked that it seems that pywin32 is Windows only, so unless you could get it working somehow with WINE, it may not be possible to use it with Linux. Very sorry about that, I was not intentionally trying to be misleading. – jesterjunk Dec 28 '15 at 10:54
-
@jesterjunk it is OK :) – Vishal Joshi Dec 28 '15 at 14:59
1 Answers
0
You can do that using a method outlined in my answer to a similar question.
It does not require Excel to be installed and, because it's pure Python, it's cross-platform too!
msoffcrypto-tool supports password-protected (encrypted) Microsoft Office documents, including the older XLS binary file format.
Install msoffcrypto-tool:
pip install msoffcrypto-tool
You could create an unencrypted version of the workbook from the command line:
msoffcrypto-tool Myfile.xlsx Myfile-decrypted.xlsx -p "caa team"
Or, you could use msoffcrypto-tool as a library. While you could write an unencrypted version to disk like above, you may prefer to create an decrypted in-memory file and pass this to your Python Excel library (
openpyxl
,xlrd
, etc.).import io import msoffcrypto import openpyxl decrypted_workbook = io.BytesIO() with open('Myfile.xlsx', 'rb') as file: office_file = msoffcrypto.OfficeFile(file) office_file.load_key(password='caa team') office_file.decrypt(decrypted_workbook) # `filename` can also be a file-like object. workbook = openpyxl.load_workbook(filename=decrypted_workbook)

eidorb
- 469
- 1
- 5
- 8