Workbook protection in openpyxl
I am trying to secure an Excel workbook using openpyxl.
So far I have been looking into various potentially relevant classes, but I cannot find a method set_password
, for example, for Worksheets .
There is a workbook.protection that I tried my luck with. My code, reduced to an absolute relevant minimum, looks like this:
from openpyxl import Workbook
from openpyxl.workbook.protection import WorkbookProtection
wb = Workbook()
wb.security = WorkbookProtection(workbookPassword='0000', revisionsPassword = '0000', lockWindows = True, lockStructure = True, lockRevision = True)
wb.create_sheet("testSheet")
wb.save("test.xlsx")
I have no errors, but no protection. Any help would be much appreciated.
+3
source to share
1 answer
This currently does not work in Openpyxl (v2.4.8) - the password is not written to the output file. There is an open PR for this ( https://bitbucket.org/openpyxl/openpyxl/pull-requests/180/read-and-write-workbook-protection/diff ) so hopefully it will be included in future releases.
+2
source to share