How to open a password protected excel file using python?

Solution 1:

I don't think that named parameters work in this case. So you'd have to do something like:

xlwb = xlApp.Workbooks.Open(filename, False, True, None, password)

See http://msdn.microsoft.com/en-us/library/office/ff194819.aspx for details on the Workbooks.Open method.

Solution 2:

I recently discovered a Python library that makes this task simple.

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)
    

Solution 3:

If your file size is small, you can probably save that as ".csv". and then read

It worked for me :)