Workbook and worksheet protection fails

Issue #1020 resolved
Tristan Brown
created an issue

Tried:

from openpyxl import Workbook

def _create_template(headers):
    template = Workbook()
    sheet = template.active
    sheet.append(headers)
    template.security.workbook_Password = 'pw'
    sheet.protection.sheet = True
    sheet.protection.password = 'pw'
    template.save(filename='template.xlsm')

When I run this function on a list of column headers and open template.xlsm, all cells are editable without a password. In addition to the above, I tried setting cell.protection = Protection(locked=True) for the cells in the sheet, with no further success.

Comments (4)

  1. Tristan Brown reporter
    #!/usr/bin/env python3
    from openpyxl import Workbook
    
    def create_template(headers):
        template = Workbook()
        sheet = template.active
        sheet.append(headers)
        template.security.workbook_Password = 'pw'
        sheet.protection.sheet = True
        sheet.protection.password = 'pw'
        template.save(filename='template.xlsm')
    
    headers = ['NAME', 'DATA', 'OTHER DATA']
    
    create_template(headers)
    

    I can run that script and 'template.xlsm' is fully editable.

  2. Log in to comment