0

I have a required where I need to detect if the excel file is password protected or not. If password protected ask user for the password. How do I do this in python? I looked a bit on the internet, none of the answers were useful so far

moken
  • 3,227
  • 8
  • 13
  • 23
Prajna
  • 578
  • 4
  • 8
  • 23
  • Are you using openpyxl? If so, this might help: https://stackoverflow.com/questions/54558302/how-to-check-if-a-large-excel-file-is-protected-as-quickly-as-possible – Pragmatic_Lee May 13 '23 at 11:18
  • I have already gone through that thread. It gives `zipfile.BadZipFile: File is not a zip file` while loading the workbook. so none of the answer helped me @Pragmatic_Lee – Prajna May 13 '23 at 11:30

2 Answers2

1

You could do something along these lines I guess.
If the file is encrypted it will pass and the password can be requested.
If it's not, it will error and the file can be loaded normally.

import openpyxl
import io
import msoffcrypto
from msoffcrypto.exceptions import FileFormatError  # pip install msoffcrypto-tool

filename = 'foo.xlsx'

with open(filename, 'rb') as file:
    try:
        workbook = io.BytesIO()
        office_file = msoffcrypto.OfficeFile(file)
        print("File is password protected")
        passwd = input("Please enter file password")
        office_file.load_key(password=passwd)
        office_file.decrypt(workbook)
    except FileFormatError as e:
        print(e)
        print("File is not password protected")
        workbook=filename

    wb = openpyxl.load_workbook(workbook)
    ws = wb['Sheet1']

    print(ws['A1'].value)
moken
  • 3,227
  • 8
  • 13
  • 23
0

after digging into the library I found the solution - https://github.com/nolze/msoffcrypto-tool/blob/03d3298f7b8e27a26280bab1702bad36cce2e398/msoffcrypto/__main__.py#L23

import msoffcrypto
file_c = msoffcrypto.OfficeFile(content) 
is_pwd_protected = file_c.is_encrypted()  #is_encrypted returns True if the file is password protected.
nolanding
  • 103
  • 8