Simple Python Excel password crack - Brute Force
19:56 05 Feb 2019

For a demonstration I want to crack an excel file (named 'xl') password. I use the following code but (1) it fails to open excel and (2) it does not stop running when the password is cracked.

import itertools
import string 
from win32com.client import Dispatch

file = input('Path: ')

chars = string.ascii_lowercase + string.digits

for password_length in range(1, 2):
    for password in itertools.product(chars, repeat=password_length):
        password = ''.join(password)

        print ('Testing password: '+ password)
        instance = Dispatch ('Excel.Application')

        try:
            instance.Workbooks.Open(file, False, True, None, password)
            print ('Password Cracked: ' + password)
            #break

        except:
            pass  

I set the excel password as 'p' and the code just runs through 1 character combinations for simplicity. Moreover, when you run the code it requires as input the path of the excel file.

I can't figure out my mistake and I would appreciate some help. Also if doing this for a word document is easier please let me know.

python excel passwords