0

I am working on BOT where it needs to know if the excel workbook is password protected or not (using python or robot framework). Is there any library or trick in either of the them to get it done as I have been doing R&D on it since last many days but have found nothing. Every solution I encountered tells me how to read password protected excel but I don't want to read the content because BOT just needs to send an email if the given excel is password protected.

Thanks in advance.

Amandeep Kaur
  • 63
  • 1
  • 4
  • Try this: https://stackoverflow.com/questions/54558302/how-to-check-if-a-large-excel-file-is-protected-as-quickly-as-possible – little_monkey Apr 28 '20 at 07:47
  • I have tried using xlrd, openpyxl and pandas libraries of python in order to get proper error message related to password protection but openpyxl gives me - "File is not a zip file" error while the other two give me - "Can't find workbook in OLE2 compound document" error on just load workbook step. These errors seems generic and might as well occur for some other scenario. I want to be absolutely sure that excel is password protected so Can I get some problem relevant error message using some library or maybe there is some workbook property I can access to ensure the password protection ? – Amandeep Kaur Apr 28 '20 at 08:03

1 Answers1

1

I have found a solution to this issue - Python has a library msoffcrypto-tool which helped in achieving what I need. Following is the code snippet for the same.

def isExcelEncrypted(excelPath):
    try:
        fileHandle = open(excelPath, "rb")
        ofile = msoffcrypto.OfficeFile(fileHandle)
        isEncrypted = ofile.is_encrypted()
        fileHandle.close()
        return isEncrypted
    except Exception as err:
        return "Exception: "+ str( format(err) )

Although the library is for decrypting MS Office files, I used its is_encrypted() function (returns True/False) only and also, it worked for both .xls and .xlsx formats.

Amandeep Kaur
  • 63
  • 1
  • 4