Password Protecting Excel file using Python

前端 未结 3 555
[愿得一人]
[愿得一人] 2020-12-03 16:34

I havent found much of the topic of creating a password protected Excel file using Python.

In Openpyxl, I did find a SheetProtection module using:

3条回答
  •  情话喂你
    2020-12-03 17:08

    Looking at the docs for openpyxl, I noticed there is indeed a openpyxl.worksheet.SheetProtection class. However, it seems to be already part of a worksheet object:

    >>> wb = Workbook()
    >>> ws = wb.worksheets[0]
    >>> ws.protection
    
    

    Checking dir(ws.protection) shows there is a method set_password that when called with a string argument does indeed seem to set a protected flag.

    >>> ws.protection.set_password('test')
    >>> wb.save('random.xlsx')
    

    I opened random.xlsx in LibreOffice and the sheet was indeed protected. However, I only needed to toggle an option to turn off protection, and not enter any password, so I might be doing it wrong still...

提交回复
热议问题